【问题标题】:UINavigationItem is not shown in my ViewControllerUINavigationItem 未显示在我的 ViewController 中
【发布时间】:2013-08-02 15:09:22
【问题描述】:



我正在使用 Xcode 在 iOS 中编写一个简单的应用程序,我正在尝试加载另一个 ViewController 作为模式。我正在加载模式的来源HomeScreenViewController(继承自UIViewController)源自项目的故事板。
然后,作为对按钮按下事件的响应,我正在像这样加载这个模式:

-(IBAction)onAddButtonPressed:(UIButton *)sender {
    MyAnotherViewController *vc = [[MyAnotherViewController alloc] init];
    [self presentViewController:vc animated:YES completion:nil];
}

MyAnotherViewController 类在 Storyborad 中没有表示,因为它是一个显示导航栏和文本字段的简单类。代码为(部分代码,其余为Xcode自动生成的方法):

@implementation MyAnotherViewController 

- (void)viewDidLoad {
    [self.navigationItem setTitle:@"Example"];
    [self.view addSubview:[[UITextView alloc]initWithFrame:self.view.bounds]];
}
@end

问题是(也可以在附图中看到)navigationItem 由于某种原因没有显示。
我还验证了self.navigationItem 不是nil,它不是。更重要的是,我可以在调试模式下看到标题实际上设置为“示例”。



非常感谢您的帮助,
干杯...

【问题讨论】:

  • 您好,当我们将视图控制器加载为模型时,导航栏每次都会隐藏,您需要通过添加工具栏和栏按钮项来管理它。
  • 感谢您的留言 :) 下面的 Vinzzz 也给了我确切的答案!

标签: ios uinavigationcontroller uinavigationitem ios6.1


【解决方案1】:

UIViewController 的 UINavigationItem 属性仅在 ViewController 位于 UINavigationController 内时使用,因此:

-(IBAction)onAddButtonPressed:(UIButton *)sender {
    MyAnotherViewController *vc = [[MyAnotherViewController alloc] init];
    UINavigationController *navCtl = [[UINavigationController alloc] initWithRootController:vc];
    [self presentViewController:navCtl animated:YES completion:nil];
}

【讨论】:

    【解决方案2】:

    如果您的MyAnotherViewController 不是UINavigationController 的子类,或者如果您没有手动添加UINavigationItemUIViewController 将无法显示导航项。也许您可以尝试用UINavigationController 包装MyAnotherViewController

    // Assume you have adopted ARC
    -(IBAction)onAddButtonPressed:(UIButton *)sender {
        MyAnotherViewController *vc = [[MyAnotherViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
        [self presentViewController:nav animated:YES completion:nil];
    }
    

    在您的-viewDidLoadMyAnotherViewController 中,您只需要这样做:

    -(void)viewDidLoad {
        self.title = @"Example";
        /*
         * Your other code
         */
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多