如何通过UINavigationController设置一个navigationController?

发布于 2024-10-19 06:13:08 字数 1151 浏览 3 评论 0原文

我正在处理推送通知。当单击通知中的操作按钮时,我试图在导航控制器中创建并推送 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浮世清欢 2024-10-26 06:13:08

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

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

RootViewController *controller = [[RootViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
//Here you would display navigationController.view somehow

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

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];

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

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:

RootViewController *controller = [[RootViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:controller];
//Here you would display navigationController.view somehow

So, after you have your root view controller in a navigation controller, in a method in root view controller you can do this:

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];

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.

温柔嚣张 2024-10-26 06:13:08

如果您的导航控制器为零,则需要创建它,然后将东西放入其中。
试试这个:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[navController pushViewController:detailViewController animated:NO]; // or YES

If your nav controller is nil, it needs creating and then you place things into it.
Try this:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[navController pushViewController:detailViewController animated:NO]; // or YES
纸伞微斜 2024-10-26 06:13:08

如果一开始没有 UINavigationController 并且希望在其堆栈中显示多个视图控制器,则在初始化导航控制器后,您可以将 viewControllers 属性设置为包含所需的各种视图控制器的数组。

编辑:一些示例代码:

UINavigationController *navController = [[UINavigationController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
[navController setViewControllers:viewControllers animated:NO];

完成此操作后,您的导航控制器将在顶部显示 viewController2,在其后面显示 viewController1。

或者,您可以这样做:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController1];
[navController pushViewController:viewController2 animated:NO];

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:

UINavigationController *navController = [[UINavigationController alloc] init];
NSArray *viewControllers = [NSArray arrayWithObjects:viewController1, viewController2, nil];
[navController setViewControllers:viewControllers animated:NO];

After doing that, your navigation controller will have viewController2 on top and viewController1 behind it.

Alternately, you could do it this way:

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController1];
[navController pushViewController:viewController2 animated:NO];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文