繁体   English   中英

无法在iOS中获得任何导航项目

[英]Can't get any navigation item in iOS

我对导航栏有疑问。

据我从iOS上了解到:由segue打开的视图控制器继承了父视图控制器的导航栏。 到目前为止,这正确吗?

堆栈中是否有视图控制器“拥有”复杂的segue堆栈中的导航栏(例如,打开的TabViewController打开的TableViewController ...)?

我经常遇到这样的问题,即我不知道在哪里获得实际的导航项以设置标题或条形按钮项。

在这种情况下,我具有以下控制器:

  • TabBarController
  • EventPostsViewController >要显示帖子列表,是TabBarController中的选项卡式视图
  • CreatePostViewController >撰写新文章

因此,在EventPostsViewController我可以执行此操作(并且可以运行):

class EventPostsViewController: UITableViewController {
    ...
    override func viewWillAppear(animated: Bool) {
        ...
        // This solution works, but only for EventPostsViewController
        self.tabBarController?.navigationItem.title = "text"

但是在CreatePostViewController内(由EventPostsViewController通过EventPostsViewController打开)中,这两种解决方案都不起作用。

class CreatePostViewController: UIViewController {
    ...
    override func viewWillAppear(animated: Bool) {
        ...
        // Neither of these solutions works
        self.navigationItem.title = "Text"
        self.tabBarController?.navigationItem.title = "Text"
        self.navigationController?.navigationItem.title = "Text"

如何获得实际的导航栏/ navigationItem?

我每次都会重复的愚蠢简单错误:)

我忘记了使用界面生成器将自定义CreatePostViewController与视图控制器链接。

在此处输入图片说明

该代码现在可以工作:

class CreatePostViewController: UIViewController {
    ...
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated);
        self.navigationController?.setNavigationBarHidden(false, animated: false)

        // Set title
        self.navigationItem.title = "Write Post"

        // Add Submit button
        var submitButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: "submitPost:")
        self.navigationItem.rightBarButtonItem = submitButton
    }
    ...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM