如何通过UINavigationController设置一个navigationController?
我正在处理推送通知。当单击通知中的操作按钮时,我试图在导航控制器中创建并推送 DetailView 。但 navigationController 为零。我如何将该 DetailView 放入 navigationController 中?我想将 RootViewController 推入 navigationController,然后推入 DetailView。我怎样才能做到这一点?
在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];
}
在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];
这是我现在使用的代码。请注意我有
[self.tableView reloadData];
I am working with push notifications. i am trying to create and push a DetailView in the navigationController when action button in notification is clicked. but navigationController is nil. how can i put that DetailView in the navigationController? I want to push RootViewController in the navigationController and then the DetailView. how can i do that?
in 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];
}
in 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];
this is the code i m using right now. and plz notice that i have
[self.tableView reloadData];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,现在我很确定我理解了这个问题。问题是,您永远不会在 UIViewController 上手动设置 navigationController 属性。如果视图控制器不在导航控制器下,则 navigationController 属性为零;如果在导航控制器下,则该属性指向它。
您需要做的是,当您显示根视图控制器时,不要直接显示其视图,而是将其添加到导航控制器,然后显示导航控制器的视图。像这样:
因此,在将根视图控制器放入导航控制器后,在根视图控制器中的方法中,您可以执行以下操作:
关键是:您需要先将根视图控制器放入导航控制器中,然后才能从根视图控制器中访问导航控制器。
Okay, now I am pretty sure I understand the issue. The problem is, you never manually set the navigationController property on a UIViewController. The navigationController property is nil if the view controller is not under a navigation controller and if it is, then the property points to it.
What you need to do, is when you display your root view controller, instead of directly displaying its view, add it to a navigation controller, then display the navigation controller's view. Like so:
So, after you have your root view controller in a navigation controller, in a method in root view controller you can do this:
The key thing is this: you need to put the root view controller into a navigation controller before you can access a navigation controller from within root view controller.
如果您的导航控制器为零,则需要创建它,然后将东西放入其中。
试试这个:
If your nav controller is nil, it needs creating and then you place things into it.
Try this:
如果一开始没有 UINavigationController 并且希望在其堆栈中显示多个视图控制器,则在初始化导航控制器后,您可以将 viewControllers 属性设置为包含所需的各种视图控制器的数组。
编辑:一些示例代码:
完成此操作后,您的导航控制器将在顶部显示 viewController2,在其后面显示 viewController1。
或者,您可以这样做:
If you start out with no UINavigationController and want to display it with more than one view controller in its stack, after initing the navigation controller you can set the viewControllers property to an array with the various view controllers you want.
edit: some example code:
After doing that, your navigation controller will have viewController2 on top and viewController1 behind it.
Alternately, you could do it this way: