问题
I've been searching for a few hours and haven't found any answers, so I would really appreciate your help!
I am designing an app and need to segue to a different view controller when a button in an alert is pressed. I already have the alert set up, but just need the code to segue to a new view.
回答1:
Try this. create alertview first.
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Open New controller?" delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes",nil];
[alertView show];
[alertView release];
Implement the AlertView Delegate Method
#pragma mark AlertView Delegate
-(void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex != alertView.cancelButtonIndex)
{
Viewcontroller *vc = [[UIViewcontroller alloc] initWithNib:@"Viewcontroller"];
[self presentModalViewController:vc];
[vc release];
}
}
回答2:
Implement the UIAlertViewDelegate
and add the method alertView:clickedButtonAtIndex:
. Once the right button is clicked, call the method to segue into the new view.
回答3:
Implement UIAlertViewDelegate
and then you get a callback with which button was pressed when the alert was dismissed. Simply call performSegueWithIdentifier
in there.
More details at the UIAlertViewDelegate protocol reference
来源:https://stackoverflow.com/questions/11957433/ios-dev-segue-by-pressing-a-button-in-an-alert