【问题标题】:Crash when adding UITabBarController to window iOS 6将 UITabBarController 添加到窗口 iOS 6 时崩溃
【发布时间】:2012-09-27 13:07:11
【问题描述】:

在我将 XCode 更新到 4.5 版和 iOS 6 SDK 之前,应用程序运行良好。

应用程序因以下输出而崩溃:

*** Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency',    reason: 'A view can only be associated with at most one view controller at a time! View   <UIView: 0xad397b0; frame = (0 20; 320 460); autoresize = W+H; layer = <CALayer: 0xad39810>> is associated with <UIViewController: 0xad39940>. Clear this association before associating this view with <RootViewController: 0x9e62f00>.'
*** First throw call stack:
(0xda012 0x1ea7e7e 0xd9deb 0xbc9309 0xc605ac 0xc5ca90 0x1661e8e 0x16619b7 0x168c428     0xd980cc 0x1ebb663 0xd545a 0xd96bcf 0xc5be37 0xc5c418 0xc5c648 0xc5c882 0xc5cb2a 0xc73ef5   0xc73fdb 0xc74286 0xc74381 0xc74eab 0xc74fc9 0xc75055 0xd7a3ab 0xbcb92d 0x1ebb6b0 0x405fc0   0x3fa33c 0x3fa150 0x3780bc 0x379227 0x41bb50 0xb9152f 0xa2afe 0xa2a3d 0x807c2 0x7ff44 0x7fe1b 0x2bef7e3 0x2bef668 0xb7b65c 0x2a6b 0x29d1)
libc++abi.dylib: terminate called throwing an exception

应用程序正在添加到窗口登录视图,然后是带有此代码的标签栏视图:

/* LOGIN VIEW ADD */

[window addSubview:[loginVC view]];
[window makeKeyAndVisible];

/* LOGIN VIEW REMOVE AND ADD TAB BAR CONTROLLER */

[loginVC.view removeFromSuperview];
[window addSubview:[tabBarController view]];
[window makeKeyAndVisible];

删除和添加后应用程序崩溃。

苹果在 iOS 6 上做了什么?为什么会崩溃?

【问题讨论】:

    标签: iphone objective-c xcode ios6


    【解决方案1】:

    您不应再将视图添加为主窗口中的子视图。现在,正确的做法是将您的rootController 设置为您想要的UIViewController,即根目录。

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        UIViewController *myViewController = [[UIStoryboard storyboardWithName:@"RandomStoryboard" bundle:nil] instantiateInitialViewController];
        window.rootViewController = myViewController;
        return YES;
    }
    

    不过,大多数应用程序都可以通过简单的方式做到这一点。如果您的项目中有一个主故事板(在项目文件中设置),它将自动从该故事板实例化初始视图控制器。

    【讨论】:

    • 谢谢,但我找到了问题所在。 viewcontroller 的 xib 必须是 view 而不是 viewcontroller
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 2014-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多