2012-07-30 69 views
0

從第一個警報的按鈕執行第二個警報,這基本上是一個確認來打電話給某人。我沒有得到任何錯誤,但它不起作用。
當我按下第二警報呼叫按鈕,它崩潰AlertView裏面的alertView

-(void) alertView: (UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 

    NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex]; 
    if ([buttonString isEqualToString:@"Phone"]) 
    {  
      UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Notice!" message:@"You are about to call .... Do you wish to continue?" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Call", nil];  
      [alert2 show]; 

      if ([buttonString isEqualToString:@"Call"]){ 

       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://12345678"]]]; 

      } 

      if([buttonString isEqualToString:@"Website"]){ 

       [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"website"]]; 
      } 

      if ([buttonString isEqualToString:@"Facebook"]){ 

      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.facebook.com/groups/..../"]]; 

      } 
    } 
} 

回答

0

推出第二警報有延遲。問題是 - 警報需要一些時間來隱藏自身,才能出現任何新的警報,因此直接調用第二個警報是行不通的。

嘗試這個,例如:

- (void) alertView: (UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

    NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex]; 
    if ([buttonString isEqualToString:@"Phone"]) 
    { 
     [self performSelector:@selector(callSecondAlertView) withObject:nil afterDelay:1]; 
    } 
} 

- (void)callSecondAlertView 
{ 
    //show new alert view 
    UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Notice!" message:@"You are about to call .... Do you wish to continue?" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"Call", nil]; 

    [alert2 show]; 

    [alert2 release]; 
} 

如果不工作,或過長時間的延遲 - 與afterDelay:價值發揮。

+0

現在它會立即調用,而不會彈出第二個提醒。我嘗試了5秒..我的方式,它確實帶來了第二次警報,但沒有執行電話.. – snksnk 2012-07-30 12:00:52

+0

好吧,你試過,如果該電話甚至工作? 012UR [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@「tel:// 12345678」]]]; – 2012-07-30 12:06:58

+0

是調用工作..在第二次提醒之前測試它 – snksnk 2012-07-30 12:18:59

1

您在第二個alertview中被分配給代表爲零。這就是爲什麼alertView:clickedButtonAtIndex:委託方法沒有被第二次調用。所以你應該把這個委託分配給self

-(void) alertView: (UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

        NSString *buttonString = [alertView buttonTitleAtIndex:buttonIndex]; 

        if([buttonString isEqualToString:@"Phone"]) { 

        UIAlertView *alert2 = [[UIAlertView alloc] initWithTitle:@"Notice!" message:@"You are about to call .... Do you wish to continue?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Call", nil]; 
        [alert2 show]; 
        } 

        if([buttonString isEqualToString:@"Call"]){ 

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://12345678"]]]; 
        } 

        if([buttonString isEqualToString:@"Website"]){ 

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"website"]]; 
        } 


        if([buttonString isEqualToString:@"Facebook"]){ 

        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.facebook.com/groups/..../"]]; 
        } 

} 

我認爲這會對您有所幫助。

+0

如果我將代理更改爲自我,它會得到一個SIGABRT錯誤。 – snksnk 2012-07-30 12:21:48

+0

如果條件分開,我將它分開。請檢查一下。 – 2012-07-30 12:26:22

+0

仍然一樣.. – snksnk 2012-07-30 12:34:04

0

明白了!出於某種原因[[alertView子視圖] objectAtIndex:6] setBackgroundColor ...對於第二個alertView功能干擾的第一個alertView按鈕。 xcode雖然並沒有直接指示我直到問題,直到我更新xcode爲4.4.1

0

使用alertView:didDismissWithButtonIndex:委託方法而不是alertView:clickedButtonAtIndex:委託方法。警報消失後調用前者。當您想要根據第一個警報視圖的輕擊按鈕顯示第二個時,這會更有意義。

也給UIAlertview對象的標籤。