change RootviewController to Navigation controller

14,382

Solution 1

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.

Solution 2

Instead of:

[self.window addSubview:navigationController.view];

make navigationController the rootViewController of window:

 self.window.rootViewController = navigationController;

Also, is detailViewController of type UINavigationController? You cannot set UINavigationController as root to another UINavigationController object.

Solution 3

Just add this line,

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

before UINavigationController's initialization,

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;
Share:
14,382
Krunal
Author by

Krunal

iPhone/iPad developer from Mumbai (India) and you will always find me here to help you ;)

Updated on June 15, 2022

Comments

  • Krunal
    Krunal almost 2 years

    I am new to iPhone,

    I want to change my Rootviewcontroller to my new class and make it to navigation controller.

    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: