如何在ipad应用程序中的后台线程中显示UIAlertView?

时间:2013-06-26 11:59:49

标签: ios objective-c ipad uialertview

我需要在某些代码在后台执行时显示警报视图。为此,我实现了以下代码。

//this is loading alert  
-(void)showAlert:(NSString *)message {

//    UIAlertView *alert;
alert11 = [[UIAlertView alloc] initWithTitle:@"Updates" message:message delegate:self        
 cancelButtonTitle:@"OK" otherButtonTitles: nil];
   #ifndef IOS5_1
  [alert11 autorelease];
  #endif

[alert11 show];

}

 -(void) showUpdates1:(NSString *)data {
isUpdating = true;
VideoBrowserAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
[appDelegate initApplicationDefaults];
[self performSelectorOnMainThread:@selector(showAlert:) 
                       withObject:@"Please wait while Updating the view...."
                    waitUntilDone:YES];
[appDelegate openExhibitView1];
  //this is update completed alert
 [VideoBrowserAppDelegate addUpdateLog:@"Update is completed" showLog:TRUE     calledFrom:nil];

  }

但是在执行SelectorOnMainThread(..)时,会显示警报但是在第二个时间消失。之后,openExhibitView1()完全执行,并再次正确显示更新警报。当我们再次单击更新警报的确定按钮时,会显示加载警报。但这不公平。我需要显示加载警报,直到openExhibitView1()在后台执行,除非我们点击确定按钮。请帮助我如何实现这个目标。

2 个答案:

答案 0 :(得分:1)

我建议你像AlertView一样写自己。但不要忘记iOS上禁止继承alertView。

我想你创建自己的UIView子类并实现showWithMessage,title和其他方法。撰写视图然后显示它。

此外,如果你坚持使用警报..这里有一篇可能有帮助的有趣帖子......

Multithreading iOS - performing alerts

但我的建议是使用自定义显示动画将UIView子类化。 动画示例:

  - (void)animateShow
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation
                                      animationWithKeyPath:@"transform"];

    CATransform3D scale1 = CATransform3DMakeScale(0.5, 0.5, 1);
    CATransform3D scale2 = CATransform3DMakeScale(1.2, 1.2, 1);
    CATransform3D scale3 = CATransform3DMakeScale(0.9, 0.9, 1);
    CATransform3D scale4 = CATransform3DMakeScale(1.0, 1.0, 1);

    NSArray *frameValues = [NSArray arrayWithObjects:
                            [NSValue valueWithCATransform3D:scale1],
                            [NSValue valueWithCATransform3D:scale2],
                            [NSValue valueWithCATransform3D:scale3],
                            [NSValue valueWithCATransform3D:scale4],
                            nil];
    [animation setValues:frameValues];

    NSArray *frameTimes = [NSArray arrayWithObjects:
                           [NSNumber numberWithFloat:0.0],
                           [NSNumber numberWithFloat:0.5],
                           [NSNumber numberWithFloat:0.9],
                           [NSNumber numberWithFloat:1.0],
                           nil];
    [animation setKeyTimes:frameTimes];

    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    animation.duration = 0.2;

    [self.layer addAnimation:animation forKey:@"show"];
}

    - (void)animateHide
    {
        CAKeyframeAnimation *animation = [CAKeyframeAnimation
                                          animationWithKeyPath:@"transform"];

        CATransform3D scale1 = CATransform3DMakeScale(1.0, 1.0, 1);
        CATransform3D scale2 = CATransform3DMakeScale(0.5, 0.5, 1);
        CATransform3D scale3 = CATransform3DMakeScale(0.0, 0.0, 1);

        NSArray *frameValues = [NSArray arrayWithObjects:
                                [NSValue valueWithCATransform3D:scale1],
                                [NSValue valueWithCATransform3D:scale2],
                                [NSValue valueWithCATransform3D:scale3],
                                nil];
        [animation setValues:frameValues];

        NSArray *frameTimes = [NSArray arrayWithObjects:
                               [NSNumber numberWithFloat:0.0],
                               [NSNumber numberWithFloat:0.5],
                               [NSNumber numberWithFloat:0.9],
                               nil];
        [animation setKeyTimes:frameTimes];

        animation.fillMode = kCAFillModeForwards;
        animation.removedOnCompletion = NO;
        animation.duration = 0.1;

        [self.layer addAnimation:animation forKey:@"hide"];

        [self performSelector:@selector(removeFromSuperview) withObject:self afterDelay:0.105];
    }

干杯,祝你好运; - )

答案 1 :(得分:0)

UIKit不是线程安全的,这意味着如果你不尝试它会更好。我自己尝试使用后台线程中的Alert View(我忘了将它们发送到主要部分)并且行为是不可预见的,有时是崩溃,有时它在-show方法之后显示得非常多。
另一点是,您不应该一次显示多个警报,它们排队等候。如果您在显示另一个时显示警报,则最后一个将覆盖第一个,但在解雇后它将再次显示第一个。
更好的是你改变了业务逻辑,UIKit +后台线程是NO NO。