导航堆栈在setViewControllers之后为零

时间:2014-08-18 11:52:45

标签: ios objective-c uinavigationcontroller pushviewcontroller

这是我的层次结构:

A - B - C - D (tabs)
|   |   |   |
|   |   |   |
----------------> AddEventViewController (can be called from any tabs)
    |
    |
    |----> SuggestFeedController (can be accessed from B only)

让我们想象一下我目前在D标签中,并调用AddEventViewController。 在此之后,我想将用户重定向到“SuggestFeedController”,但我想保留层次结构,这意味着按下后退按钮应该重定向到B而不是AddEventViewController也不是D.

我试图做的是:

UITabBarController *tabbarController = ((UITabBarController *)appDelegate.window.rootViewController);
                 [tabbarController setSelectedIndex:1]; // Selecting the B tab

UIStoryboard* storyboard=  appDelegate.window.rootViewController.storyboard;

UICategoryFeedController *cfvC = [storyboard instantiateViewControllerWithIdentifier:@"UICategoryFeedController"]; // Instantiating B view Controller

SuggestFeed *suggestFeed = [storyboard instantiateViewControllerWithIdentifier:@"SuggestFeed"];


NSArray *viewControllers = [[NSArray alloc] initWithObjects:cfvC, suggestFeed, nil]; // Trying to create my own navigation stack, B first, then Suggest Feed


[self.navigationController pushViewController:suggestFeed animated:NO]; // Some guys on internet do the push, even if I don't think it's necessary because it's already in the stack


NSLog(@"nav stack1 : %@", self.navigationController.viewControllers );
/* returns stack1 : (
    "<D: 0xa627990>",
    "<AddEventViewController: 0x108cb7e0>",
    "<SuggestFeed: 0x123735f0>"
) */


[self.navigationController setViewControllers:viewControllers animated:NO];

NSLog(@"nav stack2 : %@", self.navigationController.viewControllers );
/* returns stack2 : (null) */

我做错了吗? (愚蠢的问题)

为什么stack2为nil,而我从我的数组中设置了viewControllers?

如果你能提供一些帮助,请提前感谢: - )

1 个答案:

答案 0 :(得分:1)

您是否检查过cfvC不是nil?如果@"UICategoryFeedController"与故事板中的标识符不匹配,则可能出现这种情况。我问,因为这会使viewControllers成为一个空数组,这将解释您的日志记录结果。

另一方面,我猜你有一个设置,其中UITabBarController中的每个标签都包含自己的导航堆栈(即有四个UINavigationController个实例,设置为{{标签栏控制器的1}}。其次,我假设您的示例中的viewControllers是视图控制器self。这意味着无论何时在此方法中调用D,无论选择哪个选项卡,您获得的内容都是选项卡D中的导航控制器。要在选项卡B中设置视图控制器,您必须在选项卡B中获得对导航控制器的引用。如果我的假设是正确的,您可以执行以下操作:

self.navigationController

导航控制器上设置UINavigationController *tabBNavigationController = tabbarController.viewControllers[1]; 阵列应该可以解决问题。另外,你是对的,你不想做viewControllers位。