UIAlertView无法正常运行

时间:2013-04-03 20:51:40

标签: ios objective-c xcode 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]);
        }
    }
}

1 个答案:

答案 0 :(得分: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];