UIAlertView 仅在关闭后才显示
我已经尝试解决这个问题两天了,在有人发布另一个 stackoverflow 问题之前,我已经阅读了所有这些问题,但没有一个完全涵盖了我的问题:
我有一个动态更新的 CoreData 应用程序。现在,在更新过程中,我希望弹出一个 UIAlertView 来表示正在下载更新。
所以这里是重要的代码:
AppDelegate:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[myUpdater checkForUpdatesInContext:self.managedObjectContext];
}
_
Updater Class:
- (void)checkForUpdatesInContext:(NSManagedObjectContext *)myManagedObjectContext
{
[self loadUpdateTime];
NSLog(@"Update start");
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone localTimeZone] secondsFromGMT]];
if ([now timeIntervalSinceDate:updateTime] < UPDATE_TIME_INTERVAL)
{
return;
}
[self showAlertViewWithTitle:@"Update"];
... //updating process
[self.alertView dismissWithClickedButtonIndex:0 animated:YES];
NSLog (@"Update done");
}
- (void) showAlertViewWithTitle:(NSString *)title
{
self.alertView = [[UIAlertView alloc] initWithTitle:title message:@"Daten werden aktualisiert..." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
... //design the alertView
[self.alertView show];
NSLog (@"AlertView shows");
}
因此,当我运行此命令时会发生什么:
- 启动图像显示
- NSLog“更新开始”触发
- NSLog“AlertView 显示”触发
- 屏幕变暗,但没有显示 AlertView
- 更新正在运行
- NSLog“更新完成”触发
- 启动图像消失,TabBarController 显示
- UIAlertView 显示并立即关闭,变暗的屏幕恢复正常
我希望发生什么:
- 启动图像
- TabBarController 显示
- 屏幕变暗,UIAlertView 显示
- 更新是运行
- UIAlertView 被取消,变暗的屏幕恢复正常,
我知道这是 UI 线程和主线程之类的东西。但我尝试了看起来的每一种组合,但仍然不是预期的结果。请帮忙:)
编辑:
HighlightsViewController Class:
- (void)viewDidLoad
{
[super viewDidLoad];
self.updater = [[Updater alloc] init];
[updater checkForUpdatesInContext:self.managedObjectContext];
... // other setup stuff nothing worth mentioning
}
这是调用 [super viewDidLoad]
的正确位置吗?因为它仍然无法像这样工作,所以当启动图像显示在屏幕上时,更新仍在完成。 :-((我准备放弃这个了..
I've been trying to figure this out for 2 days now, and before anyone posts another stackoverflow question, I've read them all and none of them cover my problem exactly:
I have a CoreData app that updates dynamically. Now during the update I want an UIAlertView to pop up saying that an update is being downloaded.
So here's the important code:
AppDelegate:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[myUpdater checkForUpdatesInContext:self.managedObjectContext];
}
_
Updater Class:
- (void)checkForUpdatesInContext:(NSManagedObjectContext *)myManagedObjectContext
{
[self loadUpdateTime];
NSLog(@"Update start");
NSDate *now = [NSDate dateWithTimeIntervalSinceNow:[[NSTimeZone localTimeZone] secondsFromGMT]];
if ([now timeIntervalSinceDate:updateTime] < UPDATE_TIME_INTERVAL)
{
return;
}
[self showAlertViewWithTitle:@"Update"];
... //updating process
[self.alertView dismissWithClickedButtonIndex:0 animated:YES];
NSLog (@"Update done");
}
- (void) showAlertViewWithTitle:(NSString *)title
{
self.alertView = [[UIAlertView alloc] initWithTitle:title message:@"Daten werden aktualisiert..." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
... //design the alertView
[self.alertView show];
NSLog (@"AlertView shows");
}
So here is what happens when I run this:
- Launch image shows
- NSLog "Update starts" fires
- NSLog "AlertView shows" fires
- Screen dims but no AlertView is shown
- Update is running
- NSLog "Update done" fires
- Launch image goes away and TabBarController shows up
- UIAlertView shows up and is dismissed right away and the dimmed screen returns to normal
What I would like to have happen:
- Launch image
- TabBarController shows up
- Screen dims and UIAlertView shows
- Update is running
- UIAlertView gets dismissed and dimmed screen returns to normal
I know it's something with the UI Thread and the main Thread and stuff.. But I tried every combination it seems but still not the expected result. Please help :)
EDIT:
HighlightsViewController Class:
- (void)viewDidLoad
{
[super viewDidLoad];
self.updater = [[Updater alloc] init];
[updater checkForUpdatesInContext:self.managedObjectContext];
... // other setup stuff nothing worth mentioning
}
Is this the right place to call [super viewDidLoad]
? Because it still doesn't work like this, still the update is being done while the Launch Image is showing on the screen. :-(( I'm about to give this one up..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好了,在这个原型中,一切都按照您想要的方式运行。
标题:
类:
您应该根据自己的目的进行调整,但它有效。
Here you go, in this prototype things work exactly how you want them to.
Header:
Class:
You should adjust were needed for your own purposes but it works.
您正在启动后台线程,然后立即消除警报。我建议您可以使用从后台任务发布的 NSNotification,并在启动警报的任何控制器中接收,从而触发消除警报的方法。
我发现 UIAlertView 界面不适合这种类型的用户通知,并且更喜欢使用半透明的覆盖视图和 UIActivityIndicatorView 以及给用户的通知消息。
You are starting a background thread and then dismissing the alert immediately. I would suggest that you might use an
NSNotification
, posted from the background task, and received in whichever controller starts the alert, triggering a method that dismissed the alert.I find the
UIAlertView
interface unsuitable for this type of user notice, and prefer to use a semi-transparent overlay view with aUIActivityIndicatorView
, plus an informing message for the user.您正在做:
您想要显示的警报视图是否需要加载一个此时尚未激活的视图?请参阅: http://developer.apple .com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html
类似的问题? UIAlertView 启动要显示,屏幕变暗,但它不会弹出,直到为时已晚!
You are doing a:
Isn't it so that the alertview you want to show needs a view to be loaded which isn't active yet at this point? See: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html
Similar question? UIAlertView starts to show, screen dims, but it doesn't pop up until it's too late!