UIAlertView showing up only after it's dismissed

萝らか妹 提交于 2020-01-06 09:56:23

问题


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:

  1. Launch image shows
  2. NSLog "Update starts" fires
  3. NSLog "AlertView shows" fires
  4. Screen dims but no AlertView is shown
  5. Update is running
  6. NSLog "Update done" fires
  7. Launch image goes away and TabBarController shows up
  8. UIAlertView shows up and is dismissed right away and the dimmed screen returns to normal

What I would like to have happen:

  1. Launch image
  2. TabBarController shows up
  3. Screen dims and UIAlertView shows
  4. Update is running
  5. 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..


回答1:


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.




回答2:


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 a UIActivityIndicatorView, plus an informing message for the user.




回答3:


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? 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!



来源:https://stackoverflow.com/questions/7672014/uialertview-showing-up-only-after-its-dismissed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!