0

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

//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()在后台执行,除非我们点击确定按钮。

4

2 回答 2

1

我建议你写你自己喜欢的 AlertView。但是不要忘记在 iOS 上继承 alertView 是被禁止的。

我建议您创建自己的 UIView 子类并实现方法 showWithMessage、title 和其他。组合视图,然后显示它。

此外,如果您坚持使用警报 .. 这是一个有趣的帖子,可能会有所帮助...

多线程 iOS - 执行警报

但我的建议是使用自定义显示动画对 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];
    }
于 2013-06-26T12:11:24.833 回答
0

UIKit 不是线程安全的,这意味着如果你不尝试它会更好。我自己尝试从后台线程使用警报视图(我忘了在主线程上调度它们)并且行为是出乎意料的,有时是崩溃,有时在方法之后它会显示很多-show
另一点是您不应一次显示多个警报,它们会排队。如果您在显示另一个时显示警报,则最后一个将覆盖第一个,但在解雇后它将再次显示第一个。
最好更改业务逻辑,UIKit + 后台线程是 NO NO。

于 2013-06-26T13:07:06.327 回答