我的 ViewController
中有一个 UILabel
,它有一个 NavigationController
(比方说 View Controller A),它带有一个点击手势识别器标签。当点击标签时,会出现另一个 View (我们称之为 B)。用户在 B 中选择一些文本, View 返回到 A,标签文本随选择更新。所以我在 A 和 B 之间创建了一个委托(delegate)来获得选择。问题是当 B 出现时我没有看到 NavigationBar
。有办法解决这个问题吗?
View Controller A
@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)
}
或者您可以简单地在 View Controller A 的导航 Controller 上调用 pushViewController
,如下所示:
self.navigationController?.pushViewController(sectionNameVC, animated: true)
这会将 sectionNameVC
添加到 View Controller A 的导航 Controller 堆栈中。在这种情况下,过渡动画会有所不同,sectionNameVC
将来自您的右侧。
关于ios - 委派设置后未出现导航栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48859068/