0

当我尝试关闭我的 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]);
        }
    }
}
4

1 回答 1

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];
于 2013-04-03T20:53:20.793 回答