2013-06-26 22 views
0

我需要在某些代码在后台执行时显示警报视图。为此,我实现了以下代码。如何在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]; 

    } 

不过,虽然即将performSelectorOnMainThread(..),显示警报,但它消失在第二位。之后openExhibitView1()完全执行,并再次更新警报显示正确。当我们点击更新警报的OK按钮时,再次显示加载警报。但这不公平。我需要显示加载警报,直到openExhibitView1()在后台执行,除非我们点击确定按钮。请帮助我如何能达到这一点。

+0

第一个消失可能是因为第二个出现。这是alertView属性..第一个隐藏第二个显示,然后当第二个取消,然后第一个回弹。 – rptwsthi

+0

也是你的问题是错的。你不能在后台线程上执行UI thibgs。 –

+0

正如@rptwsthi所说,所以您需要防止应用程序同时显示多个警报 – Wain

回答

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

这不是被禁止的,它只是不被支持。我已经成功地将UIAlertView分类为支持块,所以这绝对是一种选择。 –

+1

@Popeye:是的,他们这样做,他们从不说这是不允许的。你甚至可以自己引用它:“不支持子类化”。这并不意味着它不被允许。 –

+1

“知名”...?给我举一个例子,其中某人的应用因为它被分类为UIAlertView而被拒绝。不是因为它修改了它的视图层次结构,而是因为它将它分类了。同样,我在商店中有几个应用程序使用这个子类,所有这些应用程序都已被接受(包括对这些应用程序的后续更新)。 –

0

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

相关问题