2012-02-18 43 views
1

我做了一個警告:AlertView打開Safari

UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"mymaths.co.uk" message:@"This is a great website for maths exercises!! Have fun!!\n\rIf you prefer to view the website in Safari just press \"Safari\"" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: @"Safari",nil]; 
    [alert1 show]; 

正如你所看到的,我的第二個按鈕被稱爲 「野生動物園」,它是通過這個代碼授權:

-(void) alertView: (UIAlertView *)alert1: clickedButtonAtIndex:(NSInteger)buttonIndex{ 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://google.com"]]; 

}

但是現在,如果我點擊確定(取消按鈕),它會打開safari,如果我點擊Safari,它也會打開safari。 如果我寫:

-(void) alertView: (UIAlertView *)otherButtonTitles Safari: clickedButtonAtIndex:(NSInteger)buttonIndex{ 
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://google.com"]]; 

}

兩個按鈕取消。 我該如何解決這個問題?其目的是按「OK」抵消,而「野生動物園」,以打開Safari

回答

1

處理警報視圖代表們在下面的函數,將盡按鈕指標,

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    if (buttonIndex == 0) 
    { 
     NSLog(@"Cancel"); 
    } 
    else 
    { 
     NSLog(@"Safari"); 
    } 
} 
+0

不,它不工作: – Alessandro 2012-02-18 18:21:16

+0

- (空)alertView:(UIAlertView中*)報警1:clickedButtonAtIndex:(NSInteger的)buttonIndex { 如果(buttonIndex == 0) NSLog(@「@ Cancel」); { NSLog(@「@ Cancel」); } else { NSLog(@「Safari」); [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@「http://google.com」]]; } } – Alessandro 2012-02-18 18:22:00

+0

抱歉沒有意識到委託函數簽名是錯誤的!現在檢查結果。 – cocoakomali 2012-02-18 18:26:43

1

這裏就是我做了我的項目。

alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Error Title", nil) message:authMessage delegate: self cancelButtonTitle: NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Captcha Required Button", nil), nil]; 
alert.tag = captchaalert; 

我設置了一個標籤屬性爲警報,以防有多個需要打開Safari時單擊它們。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
    if (alertView.tag == captchaalert){ 
     if(buttonIndex==1){ 
      [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://accounts.google.com/DisplayUnlockCaptcha"]]; 
     } 
    } 
} 

希望這有助於

+0

不好意思告訴你,兩者都沒有工作。我發現了一個工作代碼,所以解決了問題,但是非常感謝您的時間。 – Alessandro 2012-02-18 21:42:42

+0

那真是奇怪,它在我的應用程序上運行良好。無論如何,不​​客氣。 – 2012-02-18 21:44:15