如何使用故事板在 AppDelegate 中使用自定义 Navigationcontroller

Posted

技术标签:

【中文标题】如何使用故事板在 AppDelegate 中使用自定义 Navigationcontroller【英文标题】:How to use custom Navigationcontroller in AppDelegate by using a Storyboard 【发布时间】:2012-01-08 17:56:36 【问题描述】:

我在 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)

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

感谢您的帮助!

【问题讨论】:

【参考方案1】:

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

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

【讨论】:

谢谢。现在很清楚并且有效:-)(赏金仅在 10 小时内可用,所以不用担心;o))【参考方案2】:

您可以在窗口对象上使用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];

【讨论】:

以上是关于如何使用故事板在 AppDelegate 中使用自定义 Navigationcontroller的主要内容,如果未能解决你的问题,请参考以下文章