2013-04-03 4 views
0

У меня проблема при попытке отклонить мой UIAlertView. UIAlertView запускается правильно и отображает две кнопки: «Да» и «Нет». Однако, когда я выбираю кнопку «Да», ничего не происходит. Мне интересно, что я делаю неправильно, и почему ничего не происходит, когда выбрана кнопка «Да».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

Ahh perfect! Не могу поверить, что я забыл назначить делегата. Благодаря! –

Смежные вопросы