UIAlertView无法正常运行

Sul*_*eed 0 xcode objective-c uialertview ios

当我尝试解雇我的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]);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ben*_*n M 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];
Run Code Online (Sandbox Code Playgroud)