UIAlertView 仅在它被关闭后才显示 [英] UIAlertView showing up only after it's dismissed
问题描述
我已经尝试解决这个问题 2 天了,在有人发布另一个 stackoverflow 问题之前,我已经阅读了所有内容,但没有一个完全涵盖我的问题:
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:
我有一个动态更新的 CoreData 应用程序.现在,在更新过程中,我希望弹出一个 UIAlertView,提示正在下载更新.
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.
这里是重要的代码:
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:
- 启动图片展示
- NSLog更新开始"触发
- NSLogAlertView 显示"触发
- 屏幕变暗但没有显示 AlertView
- 更新正在运行
- NSLog更新完成"触发
- 启动图像消失,TabBarController 出现
- UIAlertView 出现并立即消失,变暗的屏幕恢复正常
我希望发生的事情:
- 启动图片
- TabBarController 出现
- 屏幕变暗和 UIAlertView 显示
- 更新正在运行
- UIAlertView 被关闭并且变暗的屏幕恢复正常
我知道它与 UI 线程和主线程以及其他东西有关.但我尝试了所有看起来的组合,但仍然不是预期的结果.请帮忙:)
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 :)
HighlightsViewController Class:
- (void)viewDidLoad
{
[super viewDidLoad];
self.updater = [[Updater alloc] init];
[updater checkForUpdatesInContext:self.managedObjectContext];
... // other setup stuff nothing worth mentioning
}
这是调用 [super viewDidLoad]
的正确位置吗?因为它仍然不能像这样工作,当启动图像显示在屏幕上时,更新仍在完成.:-((我要放弃这个了..
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..
推荐答案
开始吧,在这个原型中,一切都按照你想要的方式工作.
Here you go, in this prototype things work exactly how you want them to.
标题:
#import <UIKit/UIKit.h>
@interface AlertViewProtoViewController : UIViewController
{
}
- (void) showAlertViewWithTitle:(NSString *)title;
- (void) checkForUpdatesInContext;
- (void) update;
- (void)someMethod;
- (void)someOtherMethod;
@end
#import "AlertViewProtoViewController.h"
类:
@implementation AlertViewProtoViewController
UIAlertView *alertView;
bool updateDone;
UILabel *test;
bool timershizzle;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor yellowColor];
UILabel *test = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
test.backgroundColor = [UIColor blueColor];
[self.view addSubview:test];
[self performSelector:@selector(checkForUpdatesInContext) withObject:nil afterDelay:0.0];
}
- (void)update
{
//NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; //commented for auto ref counting
NSLog(@"update start");
//your update stuff
NSLog(@"update end");
updateDone = YES;
//[pool release];
}
- (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"];
//[self setManagedObjectContext:myManagedObjectContext];
[self performSelector:@selector(someMethod) withObject:nil afterDelay:0.0];
[self performSelector:@selector(someOtherMethod) withObject:nil afterDelay:0.0];
}
-(void)someOtherMethod
{
while (!updateDone) {
// NSLog(@"waiting...");
}
[alertView dismissWithClickedButtonIndex:0 animated:YES];
NSLog (@"Update done");
self.view.backgroundColor = [UIColor greenColor];
}
-(void)someMethod
{
[self performSelectorInBackground:@selector(update) withObject:nil];
}
- (void) showAlertViewWithTitle:(NSString *)title
{
alertView = [[UIAlertView alloc] initWithTitle:title message:@"Daten werden aktualisiert..." delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
alertView.frame = CGRectMake(100, 100, 200, 200);
alertView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:alertView];
[self.view setNeedsDisplay];
NSLog (@"AlertView shows");
}
@end
您应该根据自己的目的进行调整,但它确实有效.
You should adjust were needed for your own purposes but it works.
这篇关于UIAlertView 仅在它被关闭后才显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!