我正在尝试以编程方式呈现UITableViewController
和UINavigationController
。但是,它会在没有导航控制器的情况下显示。我该如何解决这个问题?
- (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;
}
答案 0 :(得分:1)
你的错误在于
UINavigationController *nav = ((UINavigationController*)myt);
这不会将您的控制器嵌入导航控制器中。您只是将控制器转换为导航控制器。
试试这个:
- (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;
}