iOS 中的 UINavigationController 标题似乎已损坏
UINavigationController title in iOS seems corrupted
当我以模态方式呈现 UINavigationController,然后将一些 UIViewcontroller 推送(无动画)到其上时,最终显示的 navigationItem.title 与我预期的不同。
我试图缩小我的问题范围,并想出了以下简短代码来重现该问题:
@implementation DummyRootVC
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UINavigationController* nc = [[UINavigationController alloc] initWithRootViewController:[[Dummy1VC alloc] init]];
[self presentViewController:nc
animated:false
completion:nil];
}
@end
@implementation Dummy1VC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
self.navigationItem.title= @"DUMMY1";
[self.navigationController pushViewController:[[Dummy2VC alloc] init]
animated:false];
}
@end
@implementation Dummy2VC
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title= @"DUMMY2";
self.view.backgroundColor=[UIColor yellowColor];
[self.navigationController pushViewController:[[Dummy3VC alloc] init]
animated:false];
}
@end
@implementation Dummy3VC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor orangeColor];
self.navigationItem.title= @"DUMMY3";
[self.navigationController pushViewController:[[Dummy4VC alloc] init]
animated:false];
}
@end
@implementation Dummy4VC
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title= @"DUMMY4";
self.view.backgroundColor=[UIColor greenColor];
for (UIViewController* vc in self.navigationController.viewControllers) {
NSLog(@"VC Stack: %@ Title:%@",vc,vc.navigationItem.title);
}
NSLog(@"End");
}
@end
在 AppDelegate 中我设置:
self.window.rootViewController = [[DummyRootVC alloc] init];
当 运行 该代码显示的标题是“Dummy 2”并且 back-button 被命名为“Dummy 4”,而我实际上希望标题是“Dummy 4”和 back-button 命名为“Dummy 3”。
日志按预期显示正确 ViewController-Stack,背景颜色(按预期)为绿色(有趣的是,单击 "back" 后仍为绿色):
VC Stack: Title:DUMMY1
VC Stack: Title:DUMMY2
VC Stack: Title:DUMMY3
VC Stack: Title:DUMMY4
End
我可以解决这个问题,因为它似乎与 non-animation 设置以及模态呈现导航控制器有些相关,但我想了解潜在的问题......
谁能给我一些见解?这是一些错误还是我在做(正如我怀疑的那样)只是做错了什么?
谢谢!
将 push/present 从 viewDidLoad
放到 viewWillAppear
或 viewDidAppear
以便 viewController 可以正确初始化它的子视图以避免问题 :D
当我以模态方式呈现 UINavigationController,然后将一些 UIViewcontroller 推送(无动画)到其上时,最终显示的 navigationItem.title 与我预期的不同。
我试图缩小我的问题范围,并想出了以下简短代码来重现该问题:
@implementation DummyRootVC
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
UINavigationController* nc = [[UINavigationController alloc] initWithRootViewController:[[Dummy1VC alloc] init]];
[self presentViewController:nc
animated:false
completion:nil];
}
@end
@implementation Dummy1VC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor whiteColor];
self.navigationItem.title= @"DUMMY1";
[self.navigationController pushViewController:[[Dummy2VC alloc] init]
animated:false];
}
@end
@implementation Dummy2VC
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title= @"DUMMY2";
self.view.backgroundColor=[UIColor yellowColor];
[self.navigationController pushViewController:[[Dummy3VC alloc] init]
animated:false];
}
@end
@implementation Dummy3VC
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor=[UIColor orangeColor];
self.navigationItem.title= @"DUMMY3";
[self.navigationController pushViewController:[[Dummy4VC alloc] init]
animated:false];
}
@end
@implementation Dummy4VC
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title= @"DUMMY4";
self.view.backgroundColor=[UIColor greenColor];
for (UIViewController* vc in self.navigationController.viewControllers) {
NSLog(@"VC Stack: %@ Title:%@",vc,vc.navigationItem.title);
}
NSLog(@"End");
}
@end
在 AppDelegate 中我设置:
self.window.rootViewController = [[DummyRootVC alloc] init];
当 运行 该代码显示的标题是“Dummy 2”并且 back-button 被命名为“Dummy 4”,而我实际上希望标题是“Dummy 4”和 back-button 命名为“Dummy 3”。
日志按预期显示正确 ViewController-Stack,背景颜色(按预期)为绿色(有趣的是,单击 "back" 后仍为绿色):
VC Stack: Title:DUMMY1
VC Stack: Title:DUMMY2
VC Stack: Title:DUMMY3
VC Stack: Title:DUMMY4
End
我可以解决这个问题,因为它似乎与 non-animation 设置以及模态呈现导航控制器有些相关,但我想了解潜在的问题......
谁能给我一些见解?这是一些错误还是我在做(正如我怀疑的那样)只是做错了什么?
谢谢!
将 push/present 从 viewDidLoad
放到 viewWillAppear
或 viewDidAppear
以便 viewController 可以正确初始化它的子视图以避免问题 :D