ios - 委派设置后未出现导航栏

标签 ios swift uinavigationbar

我的 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/

相关文章:

ios - XCode 12 : No persisted cache on this platform

iOS 7 : Convert NSDate to string with custom timezone and get back new NSDate

swift - XCode swift : View Controller Navigation Area does not display correctly?

iphone - 我如何更改导航栏的背景颜色

objective-c - 在 UINavigationBar 中 setBackgroundImage 之后使用 barStyles

ios - NativeScript 点击带有 z-index 的元素

ios - swift : 'attempt to delete row 0 from section 0 which only contains 0 rows before the update'

swift - 尽管使用来自示例 (ArcGIS macOS SDK) 的相同格式的 URL,但未加载来自 URL 的 map

ios - 向文件添加新的目标成员身份会导致 "No such module"和 "Command CompileSwiftSources failed with a nonzero exit code"

ios - 我有一个没有背景的 png 文件,如何在 iOS 中为该图像创建清晰的彩色背景?