【问题标题】:Can't see my alertView unless I comment out automatic dismission除非我注释掉自动关闭,否则看不到我的 alertView
【发布时间】:2011-11-29 00:35:13
【问题描述】:

我正在实现一个带有活动指示器的简单警报视图,以便在应用程序执行数据库恢复时向用户显示。一切都很好,除了我看不到警报视图,除非我注释掉自动关闭行(这里的最后一个)。

好的,数据库的恢复非常快,但我仍然希望看到它,即使是一瞬间,不是吗?我可以看到在动画过程中屏幕变暗了一点,但仅此而已。我也试过放一个for循环来延长时间,时间延长了但仍然没有警报视图。

调用警报视图的方式没有任何问题,因为如果我评论解雇,我可以看到它......只有永远。有人在这里有想法吗?

在任何人说之前,我尝试将 alertView 的代表更改为 self,如 here 发布的那样,但没有帮助。

// First we prepare the activity indicator view to show progress indicator
UIActivityIndicatorView * activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];

[activityView setFrame:CGRectMake(121.0f, 50.0f, 37.0f, 37.0f)];
[activityView startAnimating];

// Then we put it in an alert view
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Loading" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[alertView addSubview:activityView];
[alertView show];

// Finally we operate the restore
[[SQLManager sharedSQL] restoreDatabase:[restoreDatabaseURL path]];

// And we can dismiss the alert view with the progress indicator
[alertView dismissWithClickedButtonIndex:0 animated:NO];

【问题讨论】:

    标签: ios5 uialertview uiactivityindicatorview


    【解决方案1】:

    我不确定为什么会发生这种情况,但我也偶尔会遇到。我得到这样的东西的一种方法是在下一次通话中使用performSelector:withObject:afterDelay:,如下所示:

    // First we prepare the activity indicator view to show progress indicator
    UIActivityIndicatorView * activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    
    [activityView setFrame:CGRectMake(121.0f, 50.0f, 37.0f, 37.0f)];
    [activityView startAnimating];
    
    // Then we put it in an alert view
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Loading" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
    [alertView addSubview:activityView];
    [alertView show];
    
    // Finally we operate the restore
    // [[SQLManager sharedSQL] restoreDatabase:[restoreDatabaseURL path]];
    [[SQLManager sharedSQL] performSelector:@selector(restoreDatabase:) withObject:[restoreDatabaseURL path] afterDelay:0.25];
    
    
    // And we can dismiss the alert view with the progress indicator
    [alertView dismissWithClickedButtonIndex:0 animated:NO];
    

    performSelector Documentation

    【讨论】:

    • 谢谢,我一回到家就试试这个,如果有效的话会告诉你的。
    • 所以,我试过了,但是没用,还是没有提示。然后我尝试了其他方法,使用解除本身的方法......这很有效!如果使用:[alertView performSelector:@selector(dismissWithClickedButtonIndex:animated:) withObject:nil afterDelay:2.0];,我可以看到延迟。我想知道我是否可能不在主线程上?必须做其他测试,但感谢您的建议!
    • @F np,你肯定在主线程上,否则 UI 调用将全部失败。
    • 是的,我检查了调试,我在主线程上。不幸的是,解雇后的电话仍然发生在......
    【解决方案2】:

    听起来它几乎是瞬间消失了。如果您添加一个 for 循环,它可能会冻结所有其他计算,这意味着您的活动视图在循环中断之前不会显示。

    您可以尝试使用 NSTimer 调用另一个函数来确定恢复是否完成;

    // Setup our the timer
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                                      target:self 
                                                    selector:@selector(checkRestore)
                                                    userInfo:nil
                                                     repeats:YES];
    
    
    // Timer function checking for completion
    -(void)checkRestore {
        if (restore is complete) {
            [alertView dismissWithClickedButtonIndex:0 animated:NO];
        }
    }
    

    【讨论】:

    • 谢谢,我会尽快尝试。会让你知道的。
    • 其实我没有注意到你的代码的含义,基于我目前的设计我不能使用它,因为我应该重写恢复数据库功能。如果真的没有其他工作,我会使用它。我相信这无论如何都会奏效,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-25
    相关资源
    最近更新 更多