2012-07-31 35 views
1

我想要兩個警報視圖僅在用戶第一次打開我的應用程序時才顯示出來 - 第一個警告視圖在第一個被解除後顯示。當它沒有被顯示之前,我已經將它設置爲僅顯示UIAlertViews,並且我不需要此幫助。在這種情況下,我需要幫助弄清楚如何在連續顯示兩個警報視圖。 (無效)alertView:(UIAlertView *)alertView didDismissWithButtonIndex :(NSInteger)buttonIndex不適用於我。在didFinishLaunchingWithOptions中連續使用兩個UIAlertView

這裏是我的代碼 - 記住這是在didFinishLaunchingWithOptions:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
BOOL didFirstLaunch = [defaults boolForKey:@"DidFirstLaunch"]; 
if (!didFirstLaunch) { 
    [defaults setBool:YES forKey:@"DidFirstLaunch"]; 

    UIAlertView *successAlert = //not important 
    [successAlert show]; 
    [successAlert release]; 

    //Somehow show second alert after the first is dismissed 
} 
+0

爲什麼' - (空)alertView:(UIAlertView中*)alertView didDismissWithButtonIndex:(NSInteger的)buttonIndex'不爲你工作?它在用戶點擊按鈕時調用,並且您可以在此方法中顯示另一個警報視圖。使用UIAlertView子類似乎對我來說過分了。 – 2012-07-31 12:38:55

+1

也許委託屬性未設置爲委託方法。那麼它不會工作。代碼可能會丟失'successAlert.delegate = self;' – 2012-07-31 12:42:53

+0

這就是所有這一切沃爾夫岡 - 謝謝!我會接受你的答案,因爲它的工作原理,但解決方案就是你剛纔陳述的一行。 – Jordan 2012-07-31 12:49:02

回答

4

我會用後GCD & blocksGCD一項非常簡單的解決方案是以防萬一上創建警報視圖另一個線程接着是主線程,回調應該是安全的,在主線程上執行)。請記住,我只是用5分鐘的時間編碼,所以你絕對應該改進代碼。有一件事有點難看,就是我的子類中覆蓋的委託參數。子類的接口可以改變一下,以便發生什麼更明顯...

無論如何,在這裏去...

首先創建的UIAlertView一個子類,使它有點類似於以下...

@interface FSAlertView() <UIAlertViewDelegate> 

@property (nonatomic, copy) void (^dismissHandler)(NSInteger buttonIndex); 

@end 


@implementation FSAlertView 

@synthesize dismissHandler = _dismissHandler; 

- (void)showWithDismissHandler:(void (^)(NSInteger buttonIndex))dismissHandler 
{ 
    self.dismissHandler = dismissHandler; 

    self.delegate = self; 

    [self show]; 
} 

// Alert view delegate 

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    dispatch_async(dispatch_get_main_queue(),^{ 

     if (_dismissHandler) 
     { 
      _dismissHandler(buttonIndex); 
     } 

    }); 
} 

現在在應用程序,我們可以創建警報的觀點類似下面的...

FSAlertView *alert1 = [[FSAlertView alloc] initWithTitle:@"Alert 1" 
               message:@"Some message" 
               delegate:nil 
             cancelButtonTitle:@"Cancel" 
             otherButtonTitles:@"Show 2nd Alert", nil]; 

[alert1 showWithDismissHandler:^ (NSInteger buttonIndex) { 

    NSLog(@"button pressed: %d", buttonIndex); 

    if (buttonIndex == 1) 
    { 
     UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Alert 2" 
                 message:@"Hi!" 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert2 show]; 
    } 

}]; 
+0

偉大的解決方案..! +1 ...! – Apurv 2012-07-31 12:35:41

1

如果我正確,那麼這明白你的問題可能有所幫助:

UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:@"Alert 1" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil]; 
    [firstAlert show]; 
    [self performSelector:@selector(test:) withObject:firstAlert afterDelay:2]; 
    [firstAlert release]; 

    UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:@"Alert 2" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil]; 
    [secondAlert show]; 
    [self performSelector:@selector(test:) withObject:secondAlert afterDelay:2]; 
    [secondAlert release]; 


-(void)test:(UIAlertView*)alert{ 
    [alert dismissWithClickedButtonIndex:-1 animated:YES]; 
} 

這將顯示兩個警告視圖一個接一個。

注意:我不確定您是否用取消按鈕解除警報,因此我在幾秒鐘後自動解除警報。

0

試試這個:

UIAlertView *firstAlert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"Message" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Ok", nil]; 
[firstAlert setTag:444]; 
[firstAlert show]; 
firstAlert = nil; 

AlertView委託方法:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex 
{ 
    switch (alertView.tag) { 
     case 444: 
     { 
      //Cancel ButtonIndex = 0 
      if (buttonIndex == 1) { 
       UIAlertView *secondAlert = [[UIAlertView alloc] initWithTitle:@"Title 2" message:@"Message2" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Dismiss", nil]; 
       [secondAlert setTag:555]; 
       [secondAlert show]; 
       secondAlert = nil; 
      } 
     } 
     break; 
     case 555: 
     { 
      if (buttonIndex == 1) { 
       NSLog(@"Code Here"); 
      } 
     } 
     break; 
    } 
} 
相關問題