看不到我的alertView,除非我注释掉自动解散

时间:2011-11-29 00:35:13

标签: ios5 uialertview uiactivityindicatorview

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

好的,数据库的恢复速度很快,但即使是暂时的,我仍然希望看到它,不是吗?我可以看到在动画期间屏幕变得有点暗,但就是这样。我也尝试使用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];

2 个答案:

答案 0 :(得分:2)

我不确定为什么会发生这种情况,但我偶尔也会遇到它。我得到类似这样的方法的一种方法是在下一次调用时使用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

答案 1 :(得分:1)

听起来它几乎是瞬间消失了。如果添加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];
    }
}