如何通过UINavigationController设置navigationController?

Pis*_*ean 2 iphone cocoa-touch objective-c uinavigationcontroller apple-push-notifications

我正在使用推送通知。单击通知中的操作按钮时,我试图在NavigationController中创建并推送DetailView。但navigationController为零。如何将那个DetailView放在navigationController中?我想在navigationController中然后在DetailView中推送RootViewController。我怎样才能做到这一点?

AppDelegate中

  - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
            RootViewController *controller = [[RootViewController alloc] init];
//getting warnings here.(Unused variable navigationController)  
     UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
    [controller doStuff];
    [controller release];
 }
Run Code Online (Sandbox Code Playgroud)

RootViewController中

  -(void)doStuff{ 
        [[self stories] removeAllObjects];
    [self startParsing];
    [self.tableView reloadData];
    DetailViewController *detail = [[DetailViewController alloc] init];
    //custom code
   [self.navigationController pushViewController:detail animated:YES];
[detail release];
Run Code Online (Sandbox Code Playgroud)

这是即时消息正在使用的代码。和请注意,我有

 [self.tableView reloadData];
Run Code Online (Sandbox Code Playgroud)

Sim*_*een 5

好的,现在我可以确定我了解这个问题。问题是,您永远不会在UIViewController上手动设置navigationController属性。如果视图控制器不在导航控制器下,则navigationController属性为nil,如果是,则该属性指向它。

您需要做的是显示根视图控制器,而不是直接显示其视图,而是将其添加到导航控制器,然后显示导航控制器的视图。像这样:

RootViewController *controller = [[RootViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
//Here you would display navigationController.view somehow
Run Code Online (Sandbox Code Playgroud)

因此,在将根视图控制器放置在导航控制器中之后,可以在根视图控制器的方法中执行以下操作:

DetailViewController *detail = [[DetailViewController alloc] init];
//Do whatever you need to do to set values on the detail view controller
[self.navigationController pushViewController:detail animated:YES];
Run Code Online (Sandbox Code Playgroud)

关键是:您需要将根视图控制器放入导航控制器中,然后才能从根视图控制器中访问导航控制器。