5

我在 AppDelegate 中遇到了关于 Navigationcontroller 的问题。我正在使用一个故事板,它看起来像这样:

故事板

由于使用推送通知,我的 AppDelegate 文件中有以下功能:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//...
}

当通知到达时,我想初始化“详细视图” - 需要一个 ID 作为参数的控制器。此 ID 是我的有效负载的一部分,因此它存在于didReceiveRemoteNotification.

我想如下:

DetailView *detail = [storyboard instantiateViewControllerWithIdentifier:@"detail"];

detail.conversationID = theID; 

[self.navigationController pushViewController:detail animated:YES];

此时我的问题是:如何获得导航控制器?我已经搜索过类似“getNavigationControllerByIdentifier”之类的函数或类似的函数,但一无所获。我无法直接实例化详细视图控制器,因为那里缺少导航栏。

我希望你明白我的意思——如果你认为我的方法完全错误,请纠正我;o)

只是另一个小信息:详细视图控制器中的后退按钮返回到表视图对我来说并不重要 - 当它使用“加载表视图”按钮链接到控制器时就足够了。

谢谢你的帮助!

4

2 回答 2

8

UINavigationController是一个UIViewController子类,也可以在情节提要中分配一个标识符。

用于-instantiateViewControllerWithIdentifier:创建UINavigationController它的根视图控制器。您可能需要在代码中实例化所有中间控制器并修改导航控制器的viewControllers属性以设置适当的导航堆栈。这样,当应用程序启动到详细视图时,他们可以找到返回的路,就好像他们通过界面手动推送一样。

于 2012-01-10T18:48:08.310 回答
4

您可以rootViewController在窗口对象上使用。

UIViewController *rootViewController = self.window.rootViewController;
// You now have in rootViewController the view with your "Hello world" label and go button.

// Get the navigation controller of this view controller with:
UINavigationController *navigationController = rootViewController.navigationController;

// You can now use it to push your next view controller:
DetailViewController *detail = [navigationController.storyboard instantiateViewControllerWithIdentifier:@"detail"];
detail.conversationID = theID; 
[navigationController pushViewController:detailViewController animated:YES];
于 2012-01-10T19:09:01.097 回答