UIAlertView not functioning correctly

那年仲夏 提交于 2019-12-02 14:43:15

问题


I have a problem when I try to dismiss my UIAlertView. The UIAlertView launches correctly, and displays two buttons: "Yes" and "No". However, when I select the Yes button, nothing happens. I am wondering what I am doing wrong, and why nothing occurs when the Yes button is selected.

My code is as follows:

-(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:


You need to set the delegate.

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];


来源:https://stackoverflow.com/questions/15797862/uialertview-not-functioning-correctly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!