除非我注释掉自动关闭,否则看不到我的alertView

发布于 2024-12-18 09:27:05 字数 1231 浏览 1 评论 0原文

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

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

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

在有人说之前,我尝试将alertView的委托更改为self,如发布的此处,但没有帮助。

// 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];

I am implementing a simple alert view with activity indicator to show to the user while the app is performing the restore of the database. Everything is fine except that I can't see the alert view, unless I comment out the automatic dismiss line (last one here).

Ok, the restore of the database is quite quick, but still I would expect to see it even if for an instant, no? I can see the screen get's a little darker during animation but that's all. I also tried putting a for loop to extend the time, the time extends but still no alert view.

There is nothing wrong in the way the alert view is called, since if I comment the dismiss, I can see it...only forever. Anyone has an idea here?

Before anyone says it, I tried changing the delegate of the alertView to self as posted here, but it didn't help.

// 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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

街道布景 2024-12-25 09:27:05

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

Im not sure why this happens, but I encounter it occasionally as well. One way I have gotten something like this to work is by using performSelector:withObject:afterDelay: on the next call, like this:

// 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

唠甜嗑 2024-12-25 09:27:05

听起来它几乎立刻就自我否定了。如果您添加 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];
    }
}

Sounds like it's dismissing itself almost instantaneously. If you add a for loop, it will likely freeze all other computations which means your activity view won't show up until the loop breaks.

You could try using a NSTimer to call another function to determine if the restore is complete or not;

// 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];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文