2013-06-26 73 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。

相關問題