xcode 如何使用 Storyboard 在 AppDelegate 中使用自定义 Navigationcontroller
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8779808/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use custom Navigationcontroller in AppDelegate by using a Storyboard
提问by Tobias Bambullis
I've got a problem concerning Navigationcontroller in AppDelegate. I'm using a storyboard, which looks like this:
我在 AppDelegate 中遇到了关于 Navigationcontroller 的问题。我正在使用一个故事板,它看起来像这样:
As a result of using Push notifications, i've got the following function in my AppDelegate File:
由于使用推送通知,我的 AppDelegate 文件中有以下功能:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
//...
}
When the notification arrives I want to initialize the "Detail View" - Controller which needs an ID as a parameter. This ID is part of my payload so it is present in didReceiveRemoteNotification
.
当通知到达时,我想初始化需要一个 ID 作为参数的“详细视图”-控制器。此 ID 是我的有效负载的一部分,因此它存在于didReceiveRemoteNotification
.
I'd like to to the follwing:
我想以下:
DetailView *detail = [storyboard instantiateViewControllerWithIdentifier:@"detail"];
detail.conversationID = theID;
[self.navigationController pushViewController:detail animated:YES];
My question at this point is: how can I get the navigation controller? I've searched for a function like "getNavigationControllerByIdentifier" or something like this, but found nothing. I can't instantiate the Detail View Controller directly because the navigationbar is missing there.
我现在的问题是:我怎样才能获得导航控制器?我搜索了像“getNavigationControllerByIdentifier”之类的函数或类似的东西,但一无所获。我无法直接实例化 Detail View Controller,因为那里缺少导航栏。
I hope you understand what I mean - if you think my approach is completly wrong please correct me ;o)
我希望你明白我的意思——如果你认为我的方法完全错误,请纠正我;o)
Just another small information: It's not important for me that the back button in the Detail View Controller goes back to the Table View - it's enough when it links to the controller with the button "Load Table View".
还有一个小信息:细节视图控制器中的后退按钮返回到表视图对我来说并不重要 - 当它通过按钮“加载表视图”链接到控制器时就足够了。
Thank you for help!
谢谢你的帮助!
回答by Mark Adams
UINavigationController
is a UIViewController
subclass and can also be assigned an identifier in the storyboard.
UINavigationController
是一个UIViewController
子类,也可以在故事板中分配一个标识符。
Use -instantiateViewControllerWithIdentifier:
to create the UINavigationController
and it's root view controller. You may need to instantiate all of the intermediate controllers in code and modify the navigation controller's viewControllers
property to set up the appropriate navigation stack. This way when the app launches into the detail view, they can find their way back as if they had manually pushed all the way through via the interface.
使用-instantiateViewControllerWithIdentifier:
创建UINavigationController
,它的根视图控制器。您可能需要在代码中实例化所有中间控制器并修改导航控制器的viewControllers
属性以设置适当的导航堆栈。通过这种方式,当应用程序启动到详细信息视图时,他们可以找到返回的路,就好像他们已经通过界面手动一直推过去一样。
回答by Guillaume
You can use rootViewController
on your window object.
您可以rootViewController
在 window 对象上使用。
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];