我一直在试图解决这个问题2天,在任何人发布另一个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");
}
所以这是我运行时发生的事情:
我希望发生什么:
我知道它是UI线程和主线程和东西的东西..但我尝试了它看起来的每一个组合,但仍然不是预期的结果。请帮助:)
编辑:
HighlightsViewController Class:
- (void)viewDidLoad
{
[super viewDidLoad];
self.updater = [[Updater alloc] init];
[updater checkForUpdatesInContext:self.managedObjectContext];
... // other setup stuff nothing worth mentioning
}
这是拨打[super viewDidLoad]
的正确位置吗?因为它仍然不能像这样工作,所以当启动图像显示在屏幕上时仍然会进行更新。 : - ((我准备给这个了..
答案 0 :(得分:1)
在这里,在这个原型中,事情正是你想要的。
部首:
#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
你应该根据自己的需要进行调整,但是有效。
答案 1 :(得分:0)
你正在做:
- (void)applicationDidBecomeActive:(UIApplication *)application
是不是因为要显示的警报视图需要加载一个尚未激活的视图?请参阅:http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html
同样的问题? UIAlertView starts to show, screen dims, but it doesn't pop up until it's too late!
答案 2 :(得分:0)
您正在启动后台线程,然后立即解除警报。我建议你可以使用从后台任务发布的NSNotification
,并在任何控制器启动警报时收到,触发一个解除警报的方法。
我发现UIAlertView
界面不适合此类用户通知,并且更喜欢使用带有UIActivityIndicatorView
的半透明叠加视图,以及用户的通知消息。