问题
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