ios - 导航堆栈在 setViewControllers 之后为零

标签 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 为零,而我已经从数组中设置了 viewControllers?

如果你能带来一些帮助,在此先感谢:-)

最佳答案

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

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

UINavigationController *tabBNavigationController = tabbarController.viewControllers[1];

这个 导航 Controller 上设置您的viewControllers 数组应该可以解决问题。另外,你是对的,你不想做 pushViewController 位。

关于ios - 导航堆栈在 setViewControllers 之后为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25362698/

相关文章:

objective-c - 无法使用 dropbox API 将文件(使用 ALAsset 库)上传到 dropbox

ios - didReceiveRemoteNotification :fetchCompletionHandler not being called when app is in background and not connected to Xcode

android - 可以在 Android 和 iOS 应用程序中嵌入 3D View 吗?

objective-c - 为什么要使用 ivar?

ios - 如何使用 iOS 5 实现 "Find My Friends"按钮外观?纽扣就好像印在皮革上

ios - 带有水平动画的Viewcontroller背景图片

objective-c - 在 Objective-C 中显示 PowerPoint 幻灯片

iphone - UIView 意外进入状态栏下方

iphone - 如何给 UINavigationController 添加阴影

ios - 嵌入 UINavigationController 时如何处理 UIView 区域减少的问题