2013-04-03 85 views
0

我嘗試關閉我的UIAlertView時出現問題。 UIAlertView正確啓動,並顯示兩個按鈕:「是」和「否」。但是,當我選擇「是」按鈕時,沒有任何反應。我想知道我做錯了什麼,以及爲什麼在選擇Yes按鈕時什麼都不會發生。UIAlertView無法正常工作

我的代碼如下:

-(IBAction)gotoURL 
{ 

    [self displaySafariDialogue]; 

} 

-(void)displaySafariDialogue 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Are you sure you wish to exit this app and launch the safari browser?" 
                message:@"Click yes to continue" 
                delegate:nil 
              cancelButtonTitle:@"Yes" 
              otherButtonTitles:@"No", nil]; 
    [alert setTag:100]; 
    [alert show]; 
} 

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

    NSLog(@"Alert view button clicked"); 

    if(alertView.tag == 100) 
    { 

     if (buttonIndex==0) 
     { 
      NSLog(@"Yes button selected"); 
      NSURL *url = [NSURL URLWithString:@"http://www.google.com"]; 

      if (![[UIApplication sharedApplication] openURL:url]) 
       NSLog(@"%@%@",@"Failed to open url:",[url description]); 
     } 
    } 
} 

回答

5

您需要設置委託。

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Are you sure you wish to exit this app and launch the safari browser?" 
               message:@"Click yes to continue" 
               delegate:self 
             cancelButtonTitle:@"Yes" 
             otherButtonTitles:@"No", nil]; 
+0

啊完美!我不能相信我忘了將代表設置爲自我。謝謝! – 2013-04-03 20:56:41