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

标签 ios objective-c ipad uialertview

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

//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() 完全执行,并再次更新正确显示的警报。当我们再次单击更新警报的“确定”按钮时,将显示加载警报。但这不公平。我需要显示加载警报,直到 openExhibitView1() 在后台执行,除非我们单击“确定”按钮。

最佳答案

我建议你编写自己的AlertView。但请不要忘记,iOS 上禁止对alertView 进行子类化。

我建议你创建自己的 UIView 子类并实现 showWithMessage、title 等方法。组合 View 然后显示它。

此外,如果您坚持使用警报..这里有一篇有趣的文章可能会有所帮助...

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

关于ios - 如何在ipad应用程序的后台线程中显示UIAlertView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17319411/

相关文章:

c++ - OpenCV : k-means clustering in iOS

Objective-C:引用创建类的类

android - Cordova:布局差异:iO 中没有边距底部

iphone - 从详细 View 返回时突出显示 tableViewCell

ios - 测试特定图像是否已复制到粘贴板

objective-c - 如何将多对多关系绑定(bind)到tableview?

ios - 如何调试 Apple 推送通知响应?

iphone - iPad:标签栏项目标题不显示

iphone - NSString 重音编码显示疯狂的字符

iOS-如何在 UITableview 中使用数组快速在一个原型(prototype) Cell 中显示 40 张图像?