【问题标题】:Why is showing a UIAlertView in application:didFinishLaunchingWithOptions: causing an error?为什么在 application:didFinishLaunchingWithOptions: 中显示 UIAlertView 导致错误?
【发布时间】:2012-02-20 21:22:57
【问题描述】:

在我的应用程序第一次运行时,我会向用户显示一个警报视图,以选择 iCloud 或本地文档存储。显示警报视图会导致以下错误:

应用程序的末尾应该有一个根视图控制器 应用程序启动wait_fences:未能收到回复:10004003

为什么会这样?如何在启动时显示警报视图而不出现此错误?

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Check the user preferences for document storage options
    if (![UserPreferencesHelper userDocumentStoragePreferencesHaveBeenCreated])
    {
        // User preferences have not been set, prompt the user to choose either iCloud or Local data storage
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Use iCloud?" 
                                                        message:@"Would you like to store data in iCloud?" 
                                                       delegate:self 
                                              cancelButtonTitle:nil 
                                              otherButtonTitles:@"No", @"Yes", nil];
        [alert show];
    }
}

** 更新 **

我应该提到我正在使用带有情节提要的 iOS 5。根视图控制器在情节提要中设置。

【问题讨论】:

    标签: iphone objective-c ios ipad ios5


    【解决方案1】:

    尝试将[alert show] 替换为:

    [alert performSelector:@selector(show) withObject:nil afterDelay:0.0];
    

    这会延迟一次通过 runloop 的警报,大概允许您的应用的控制器和情节提要在出现警报之前完成设置。

    【讨论】:

    • 达伦,谢谢!那工作得很好=)。我在这个墙上撞了一会儿……
    【解决方案2】:

    在您的应用程序结束之前 didFinishLaunchingWithOptions: 它需要设置一个 rootViewController。您可以为名为 viewController 的 ViewController 设置此属性:

        self.window.rootViewController = self.viewController;
    

    请注意,将控制器设置为 rootViewController 会自动添加视图。因此,您无需再次添加视图:

    [self setViewController:];
    

    【讨论】:

    • AtkinsonCM,我相信他们在 iOS5 中使用故事板改变了这一点。我应该在我的原始帖子中提到这一点。我会更新它...
    【解决方案3】:

    就像它说的那样,您的应用需要一个根控制器。警报显示在正常的控制器管理视图上方,因此您需要一个控制器管理视图才能显示在上方。

    【讨论】:

    • 嗯,我已经在情节提要中设置了我的 rootview 控制器。所以我猜你说的是在这一点上,当调用 applicationDidFinishLaunchingWithOptions 时,还没有创建 rootview 控制器。对吗?
    • 对,听起来好像接线在某个地方搞砸了。不是答案,但也许是在哪里看的指示? (抱歉,我对故事板一无所知。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-22
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 2021-03-26
    相关资源
    最近更新 更多