【问题标题】:UITabbarController dismiss modal UINavigationControllerUITabbarController 关闭模式 UINavigationController
【发布时间】:2012-07-04 22:22:08
【问题描述】:

我在这里遇到了一个非常有趣的问题。我的 iPhone 应用在 AppDelegate 中有一个 UITabbarController 作为 rootViewController。

如果是第一次打开app,基本都要配置好。为此,我创建了一个 UINavigationController 并告诉 tabbarController 以模态方式呈现它:

firstRun = [[firstRunViewController alloc] init];
navCtrl = [[UINavigationController alloc] initWithRootViewController:firstRun];
[[self tabBarController] presentModalViewController:navCtrl animated:NO];

配置完成后,我想摆脱 firstRunViewController。我经常使用这种技术,使用-dismissModalViewControllerAnimated:

但是在这个星座中这是行不通的。我将哪个控制器称为解雇并不重要。 我通过 tabbarController、rootViewController、当前活动的 viewController、原因 self 和其他几个控制器进行了尝试。

每次我打电话给-dismissModalViewControllerAnimated: 我都会收到这个异常:

'UIViewControllerHierarchyInconsistency', reason: 'presentedViewController for controller is itself on dismiss for: <UINavigationController:…

有人可以帮忙吗?在此先感谢,朱利安

编辑 在我的 AppDelegate 中,我使用 UITabbarController 作为主窗口的 rootViewController:

self.window.rootViewController = self.tabBarController;
[self.window makeKeyAndVisible];

然后我创建一个 UINavigationController 并告诉 UITabbarController 呈现 modalViewController:

UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:firstRun];
[[self tabBarController] presentModalViewController:navCtrl animated:NO];

当我现在在 firstViewController 上调用 -dismissModalViewControllerAnimated: 时,我收到了上面的错误。

【问题讨论】:

    标签: uiviewcontroller uinavigationcontroller uitabbarcontroller modalviewcontroller dismiss


    【解决方案1】:

    在我看来,您正在滥用 UITabbarController。这个类,即使是 UIViewController 的子类,并没有真正使用 UIViewController 基础结构。

    你想要的是你现在拥有的东西的轻微扩展。在您的 appDelegate 中创建一个新的 UIViewController 子类,并将其作为单个对象添加到数组中,并将 tabBar 的 viewControllers 设置为该数组。将您的子类的 hidesBottomBarWhenPushed 设置为 YES,以便在标签栏可见时隐藏它。

    现在您的应用将启动,您的 UIViewController 子类将成为最前面的视图。您可以将此视图设置为您想要以模态方式呈现的视图,或者您可以使用某种动画从您的子类中呈现该视图。哦,如果你使用启动视图作为你的子类的背景图片,你真的可以让它平滑过渡 - 我现在就这样做。

    当您的模态视图完成后,您可以实例化您想要显示的任何视图,并将 UITabBarController 设置为通过 tabBarController.viewControllers(或动画版本)使用这些视图。噗,你的 UIViewController 会被替换(在 ARC 下会消失)。

    【讨论】:

    • 这种作品……但我不能应用动画。我试图在最后一个模态视图控制器上添加 [UIView animateWithDuratio:]。但是动画只是没有出现。
    • 我真的很想了解问题所在。因为当我展示我的登录视图控制器时,我正在展示一个 modalViewController(日常业务)。我认为问题出在一个实例上,navigationController...
    【解决方案2】:

    我没有机会检验我的假设,但我怀疑这个问题可能取决于您过早呈现模态视图这一事实,即过早意味着在主窗口有机会设置之前打开标签栏控制器。因此,我建议进行以下更改:

    1. 创建一个方法来实例化你的导航控制器:

      - (void)initializeAndPresentNavigationController {
        UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:firstRun];
        [[self tabBarController] presentModalViewController:navCtrl animated:NO];
      }
      
    2. 不是直接从 appDidFinishLaunching 呈现导航控制器,而是异步调用上述方法:

      [self performSelector:@selector(initializeAndPresentNavigationController) withObject:nil afterDelay:0.0];
      

    这里调用方法的技巧就像我在 2 中所做的那样,对 initializeAndPresentNavigationController 的调用将被简单地推送到主循环中,并在您的应用有可能构建其初始 UI 之后执行。

    希望它对你有用。

    【讨论】:

    • 嘿塞尔吉奥!谢谢你的帮助。但是这个解决方案没有奏效。它会导致相同的错误 presentedViewController for controller is itself on dismiss for:
    【解决方案3】:

    我终于自己找到了答案! 我只是看不到树木的树木!我现在很开心! :)

    我做了很傻的事情:在设置 viewControllers 的最后一个 viewController 中,我不得不根据用户是否是管理员来更改 tabars viewControllers。所以我做了:

    appDelegate.tabBarController.viewControllers = [NSArray arrayWithObjects:appDelegate.readState,
                                                    appDelegate.navCtrl,
                                                    appDelegate.settings, nil];
    

    您可以看到我正在将 AppDelegate 的“navCtrl”添加到选项卡栏的 viewControllers。所以我试图关闭我刚刚添加到 parentViewControllers (UITabbarController) 子控制器的 viewController。

    不建议在同一时间忽略我想展示的内容! :))

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-11
      • 2010-11-24
      • 2011-11-28
      • 2011-03-25
      相关资源
      最近更新 更多