简体   繁体   English

UIAlertView仅在关闭后显示

[英]UIAlertView showing up only after it's dismissed

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: 我已经尝试了两天,并且在有人发布另一个stackoverflow问题之前,我已经阅读了所有内容,但没有一个内容能完全解决我的问题:

I have a CoreData app that updates dynamically. 我有一个动态更新的CoreData应用程序。 Now during the update I want an UIAlertView to pop up saying that an update is being downloaded. 现在,在更新期间,我希望弹出一个UIAlertView,说正在下载更新。

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: 因此,当我运行此命令时,将发生以下情况:

  1. Launch image shows 启动图像显示
  2. NSLog "Update starts" fires NSLog“更新开始”启动
  3. NSLog "AlertView shows" fires NSLog“ AlertView显示”触发
  4. Screen dims but no AlertView is shown 屏幕变暗,但未显示AlertView
  5. Update is running 更新正在运行
  6. NSLog "Update done" fires NSLog“更新完成”触发
  7. Launch image goes away and TabBarController shows up 启动图像消失,出现TabBarController
  8. UIAlertView shows up and is dismissed right away and the dimmed screen returns to normal UIAlertView出现并立即关闭,变暗的屏幕恢复正常

What I would like to have happen: 我想发生的事情:

  1. Launch image 启动影像
  2. TabBarController shows up TabBarController出现
  3. Screen dims and UIAlertView shows 屏幕变暗,UIAlertView显示
  4. Update is running 更新正在运行
  5. UIAlertView gets dismissed and dimmed screen returns to normal UIAlertView被关闭并且变暗的屏幕恢复正常

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. 我知道这与UI线程,主线程和其他东西有关。但是我尝试了每种组合,但似乎仍未达到预期的结果。 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] ? 这是打电话给[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. 在这里,您可以在此原型中按您希望的方式进行工作。

Header: 标头:

#import <UIKit/UIKit.h>

@interface AlertViewProtoViewController : UIViewController
{

}

- (void) showAlertViewWithTitle:(NSString *)title;
- (void) checkForUpdatesInContext;
- (void) update;
- (void)someMethod;
- (void)someOtherMethod;

@end

#import "AlertViewProtoViewController.h"

Class: 类:

@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. 您应该根据自己的需要进行调整,但是它可以正常工作。

You are doing a: 您正在执行以下操作:

- (void)applicationDidBecomeActive:(UIApplication *)application

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? 是不是要显示的alertview需要加载当前尚未激活的视图? See: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html 请参阅: 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! UIAlertView开始显示,屏幕变暗,但是直到为时已晚才弹出。

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. 我建议您可以使用NSNotification ,从后台任务发布,并在启动警报的任何控制器中收到,并触发一种方法来解除警报。

I find the UIAlertView interface unsuitable for this type of user notice, and prefer to use a semi-transparent overlay view with a UIActivityIndicatorView , plus an informing message for the user. 我发现UIAlertView接口不适合这种类型的用户通知,并且更喜欢将半透明的叠加视图与UIActivityIndicatorView一起使用,并为用户提供通知消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM