简体   繁体   English

将RootviewController更改为Navigation controller

[英]change RootviewController to Navigation controller

I am new to iPhone, 我是iPhone新手,

I want to change my Rootviewcontroller to my new class and make it to navigation controller. 我想将Rootviewcontroller更改为新类,然后将其更改为导航控制器。

Here is my code snippet, 这是我的代码段,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    


    UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:detailViewController];
    [self.window addSubview:navigationController.view];

    [self.window makeKeyAndVisible];


    return YES;
}

I am getting SIGABRT says 'adding a root view controller <NewClass: 0x6a8dd50> as a child of view controller: 我正在SIGABRT'adding a root view controller <NewClass: 0x6a8dd50> as a child of view controller:

Whenever u want to set: 每当您想设置:

DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:detailViewController];
self.window.rootViewController =nil;
self.window.rootViewController = navigationController;
[self.window makeKeyAndVisible];

EDIT : Directly use AppDelegate instance to set rootViewController for UIWindow as i have shown above. 编辑 :如上所示,直接使用AppDelegate instanceUIWindow设置rootViewController

Instead of: 代替:

[self.window addSubview:navigationController.view];

make navigationController the rootViewController of window : 使navigationController成为windowrootViewController

 self.window.rootViewController = navigationController;

Also, is detailViewController of type UINavigationController ? 另外, detailViewControllerUINavigationController类型? You cannot set UINavigationController as root to another UINavigationController object. 您不能将UINavigationController设置为另一个UINavigationController对象的根。

Just add this line, 只需添加这一行,

RootViewController *defaultViewController=[[RootViewController alloc]initWithNibName:@"NAME_OF_XIB" bundle:nil];

before UINavigationController's initialization, 在UINavigationController初始化之前,

RootViewController *defaultViewController=[[RootViewController alloc]initWithNibName:@"NAME_OF_XIB" bundle:nil];
UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:detailViewController];
[self.window addSubview:navigationController.view];

[self.window makeKeyAndVisible];
return YES;

将您的RootViewController更改为NavigationController。

UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController:self.detailViewController];

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

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