委派设置后未出现导航栏

Navigation bar not appearing after delegation setup

我的 ViewController 中有一个 UILabel,它有一个 NavigationController(比方说视图控制器 A),标签上附有点击手势识别器。当点击标签时,会出现另一个视图(我们称之为 B)。用户在 B 中选择一些文本,视图返回到 A,标签文本随选择更新。所以我在 A 和 B 之间创建了一个委托来获得选择。问题是当 B 出现时我没有看到 NavigationBar。有办法解决这个问题吗?

ViewController一个

@IBOutlet weak var sectionName: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()

    let sectionLabelTap = UITapGestureRecognizer(target: self, action: #selector(labelTapped(_:)))
    sectionName.isUserInteractionEnabled = true
    sectionName.addGestureRecognizer(sectionLabelTap)
}

@objc func labelTapped(_ sender: UITapGestureRecognizer) {
    let sectionNameVC = storyboard?.instantiateViewController(withIdentifier: "SectionName") as! SectionNameTableViewController
    sectionNameVC.selectionNameDelegate = self
    sectionNameVC.userData = userData
    present(sectionNameVC, animated: true, completion: nil)  
}

为了显示导航栏,UIViewController 需要 UINavigationController

您可以将sectionNameVC ViewController添加到UINavigationController中以保持当前动画。

在这种情况下,您的代码可能如下所示:

@objc func labelTapped(_ sender: UITapGestureRecognizer) {
        let sectionNameVC = storyboard?.instantiateViewController(withIdentifier: "SectionName") as! SectionNameTableViewController
        sectionNameVC.selectionNameDelegate = self
        sectionNameVC.userData = userData
        let naviagtionController = UINavigationController(rootViewController: sectionNameVC)
        present(naviagtionController, animated: true, completion: nil)
    }

或者您可以简单地在视图控制器 A 的导航控制器上调用 pushViewController,如下所示:

self.navigationController?.pushViewController(sectionNameVC, animated: true)

这会将 sectionNameVC 添加到视图控制器 A 的导航控制器堆栈中。在这种情况下,过渡动画会有所不同,sectionNameVC 将来自您的右侧。

您缺少 "Presenting" 视图控制器和 "Navigating" 视图控制器之间的概念。一旦你理解了这个概念,你就会得到答案。在这里,它是..

  1. 当您呈现 ViewController 时,您正在将堆栈容器完全替换为新的视图控制器。

STACK 包含您通过导航推送或弹出的 ViewController 的地址。

例如:

    present(sectionNameVC, animated: true, completion: nil)
  1. 另一方面,如果您通过推送导航到其他视图控制器。在这种情况下,您可以通过简单地从堆栈中弹出 ViewController 地址来返回到之前的控制器。

例如:

    self.navigationController?.pushViewController(sectionNameVC, animated: true)

   self.navigationController?.popViewController(animated: true)

所以,如果你导航的话,只有你会得到导航栏。

现在,在您的情况下,您显示的是 ViewController,因此导航栏未显示。