UIAlertView 仅在关闭后才显示

发布于 2024-12-08 13:54:38 字数 2076 浏览 0 评论 0原文

我已经尝试解决这个问题两天了,在有人发布另一个 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");
}

因此,当我运行此命令时会发生什么:

  1. 启动图像显示
  2. NSLog“更新开始”触发
  3. NSLog“AlertView 显示”触发
  4. 屏幕变暗,但没有显示 AlertView
  5. 更新正在运行
  6. NSLog“更新完成”触发
  7. 启动图像消失,TabBarController 显示
  8. UIAlertView 显示并立即关闭,变暗的屏幕恢复正常

我希望发生什么:

  1. 启动图像
  2. TabBarController 显示
  3. 屏幕变暗,UIAlertView 显示
  4. 更新是运行
  5. 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:

  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..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅浅淡淡 2024-12-15 13:54:38

好了,在这个原型中,一切都按照您想要的方式运行。

标题:

#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

您应该根据自己的目的进行调整,但它有效。

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.

心头的小情儿 2024-12-15 13:54:38

您正在启动后台线程,然后立即消除警报。我建议您可以使用从后台任务发布的 NSNotification,并在启动警报的任何控制器中接收,从而触发消除警报的方法。

我发现 UIAlertView 界面不适合这种类型的用户通知,并且更喜欢使用半透明的覆盖视图和 UIActivityIndi​​catorView 以及给用户的通知消息。

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.

梦中楼上月下 2024-12-15 13:54:38

您正在做:

- (void)applicationDidBecomeActive:(UIApplication *)application

您想要显示的警报视图是否需要加载一个此时尚未激活的视图?请参阅: http://developer.apple .com/library/ios/#documentation/uikit/reference/UIAlertView_Class/UIAlertView/UIAlertView.html

类似的问题? UIAlertView 启动要显示,屏幕变暗,但它不会弹出,直到为时已晚!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文