gpt4 book ai didi

ios - UINavigationItem 未显示在我的 ViewController 中

转载 作者:行者123 更新时间:2023-11-28 22:29:01 25 4
gpt4 key购买 nike



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

-(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 而事实并非如此。更重要的是,我可以在 Debug模式下看到标题实际上设置为“Example”。

As can be seen in the screenshot, the UITextView captures the entire screen

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

最佳答案

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];
}

关于ios - UINavigationItem 未显示在我的 ViewController 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18020327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com