简体   繁体   English

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

[英]How to show UIAlertView in background thread in ipad app?

I need to show alert view while some code is executing in background.我需要在后台执行某些代码时显示警报视图。 For this I have implemented the following code.为此,我实现了以下代码。

//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];

  }

But while coming to performSelectorOnMainThread(..), alert is shown but it is disappeared with in second.但是当来到 performSelectorOnMainThread(..) 时,显示警报但它在第二秒消失了。 After that openExhibitView1() is completely executed and again update alert shown correctly.之后openExhibitView1()完全执行并再次更新正确显示的警报。 When we click on OK button of update alert again loading alert is displayed.当我们再次单击更新警报的确定按钮时,将显示加载警报。 But this is not fair.但这并不公平。 I need to show loading alert until openExhibitView1() is executed in background unless until we click on ok button.我需要在后台执行openExhibitView1()之前显示加载警报,除非我们单击确定按钮。

I suggest you writing you own like AlertView.我建议你像 AlertView 一样编写你自己的。 But do not forget that subclassing the alertView is prohibited on iOS.但是不要忘记在 iOS 上禁止对 alertView 进行子类化。

I siggest you to create your own UIView subclass and implement methods showWithMessage, title and other.我建议您创建自己的 UIView 子类并实现 showWithMessage、title 和其他方法。 Compose the view and then display it.组合视图,然后显示它。

Moreover if you insist on using the Alerts .. here is an interesting post that might help ...此外,如果您坚持使用警报……这里有一篇有趣的文章,可能会有所帮助……

Multithreading iOS - performing alerts 多线程 iOS - 执行警报

But my suggestion is to subclass the UIView with custom display animation.但我的建议是使用自定义显示动画对 UIView 进行子类化。 Example of animations:动画示例:

  - (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];
    }

UIKit isn't thread safe, that means that it's better if you don't try. UIKit 不是线程安全的,这意味着最好不要尝试。 I tried by myself working with Alert View from a background thread ( I forgot to dispatch them on the main) and the behaviors are unexpectable, sometime a crash, sometimes it was displayed a really lot after the -show method.我自己尝试从后台线程使用警报视图(我忘记在主线程上调度它们)并且行为是出乎意料的,有时会崩溃,有时会在-show方法之后显示很多。
The other point is that you should not display more than one alert at time, they are queued up.另一点是你不应该一次显示多个警报,它们是排队的。 If you show an alert when another is displayed, the last one will cover the first, but after the dismissal it will show again the first.如果您在显示另一个警报时显示警报,则最后一个将覆盖第一个,但在解除后它将再次显示第一个。
Better you change the business logic, UIKit + background thread is a NO NO.最好你改变业务逻辑,UIKit + 后台线程是一个NO NO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM