为2个UIViewController创建一个UINavigationController
问题描述:
我有2 UIViewController
s,并希望UINavigationController具有UINavigationBar & UINavigationItem对它们。但我的代码不工作..为2个UIViewController创建一个UINavigationController
这里是我的代码:
#import "testView1.h"
#import "testView2.h"
@interface testView1()
@end
@implementation testView1
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor darkGrayColor];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self];
testView2 *detail = [testView2 new];
[navController pushViewController:detail animated:YES];
}
答
尝试嵌入在故事板导航控制器如下:
首先在故事板上选择testView1
。
选择导航控制器
和变化如下
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
/// testView2 *detail = [testView2 new];
testView2 *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"testView2 Identifier"]; // if you have add controller in storyboard
[self.navigationController pushViewController:detail animated:YES];
}
答
你不会看到的第一个视图控制器,因为你已经在导航控制器推第二视图控制器。
+0
不,我的第一个ViewController Color是darkGray,运行后我的屏幕是darkGray .. –
您使用的故事板? – Subramanian
其实你在这里做的是,你只是分配和初始化一个导航控制器与已经存在于窗口中的rootview控制器。这一步应该不在AppDelegate中,以获得正确的结果 – 2017-07-17 11:26:48
@Subramanian no我只是在故事板中创建UIViewController并将其设置为testView1的类 –