Illep Illep - 1 month ago 13
iOS Question

Navigating to UITableViewController embedded with UINavigationController

I am trying to present a

UITableViewController
and a
UINavigationController
embedded to it programatically. However, it gets displayed without the navigation controller. How can I solve this ?

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

MyTableViewController *myt = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"mytableviewControllerID"];
UINavigationController *nav = ((UINavigationController*)myt);

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

self.window.rootViewController = nav;
[self.window makeKeyAndVisible];

return YES;

}

Answer

Your mistake is doing

 UINavigationController *nav = ((UINavigationController*)myt);

This does not embed your controller in navigation controller. You are just casting your controller to be of navigation controller.

Try this:

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

MyTableViewController *myt = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"mytableviewControllerID"];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:myt];

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];

return YES;

}