I have created a UIAlertView
for an action which gives me 2 options. I want the user to be able to click on a button and have it perform a Segue.
Here's the code I have so far:
- (IBAction)switchView:(id)sender
{
UIAlertView *myAlert = [[UIAlertView alloc]
initWithTitle:@"Please Note"
message:@"Hello this is my message"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:@"Option 1", @"Option 2", nil];
[myAlert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@"Option 1"]) {
}
}
Yeah, it's not very obvious at first, you need to create a manual segue.
Select the ViewController that will do the pushing (I'm the one who pushes), and connect manual to the pushed view controller (The Pushed View controller).
Select the newly created segue, and give it a name (in my case is "segue.push.alert"
, long name for logging), and call the perform segue in the action of the alert, like:
let alert = UIAlertController(title: "My Alert", message: "Be Alerted. This will trigger a segue.", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: nil))
alert.addAction(UIAlertAction(title: "Segue", style: .Default, handler:
{
[unowned self] (action) -> Void in
self.performSegueWithIdentifier("segue.push.alert", sender: self)
}))
presentViewController(alert)
[unowned self]
should be handled with care, if the view controller can deallocate while the action is happening, you're better off with [weak self]
and then doing self?.performSegue...
if deallocation can occur.
Now, from a view controller you can simply call performSegueWithIdentifier:sender:
, in your case
// Using enums is entirely optional, it just keeps the code neat.
enum AlertButtonIndex : NSInteger
{
AlertButtonAccept,
AlertButtonCancel
};
// The callback method for an alertView
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)index
{
if (index == AlertButtonAccept)
{
[self performSegueWithIdentifier:@"segue.push.alert" sender:self];
}
}
The advantage of having the segues in this way (instead of being directly coded), is that you can still have that nice overview of your application flow, having intermixed coded segues and storyboard-loaded segues kinda defeats the purpose.
If you give your segue an identifier in your storyboard, you can do this:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@"Option 1"]) {
[self performSegueWithIdentifier:@"foo" sender:nil];
}
}
Here's another way to load a ViewController. You can use the Storyboard Identifier. Read this: What is a StoryBoard ID and how can i use this?
First set the Storyboard ID in Identity Inspector and then add the following code to your alert delegate.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSString *buttonTitle = [alertView buttonTitleAtIndex:buttonIndex];
if ([buttonTitle isEqualToString:@"Option 1"]) {
// This will create a new ViewController and present it.
NewViewController *newViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"NewViewControllerID"];
[NewViewController setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentViewController:NewViewController animated:YES completion:nil];
}
}
Hope this helps! :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With