8

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"]) {



    }
}
Andrew
  • 3,874
  • 5
  • 39
  • 67
Omar
  • 979
  • 3
  • 16
  • 26

3 Answers3

9

Yeah, it's not very obvious at first, you need to create a manual segue.

enter image description here

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).

enter image description here

iOS 8+ with Swift

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.

Old Answer

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.

Community
  • 1
  • 1
Can
  • 8,502
  • 48
  • 57
  • can you really do that from anywhere in code? I can;t seem to be able to do it from inside a custom UIView class – Radu Simionescu Jan 20 '14 at 12:36
  • @RaduSimionescu Yeah, sorry about that, edited. In your case, views shouldn't be messing with the flow of the app, views should only take care on drawing themselves. – Can Jan 20 '14 at 21:17
  • It's great for me,thanks a lot! Drag the plus button from parent controller to target controller ,not need press control. – Will Jul 03 '14 at 08:19
1

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

    }
}
woz
  • 10,888
  • 3
  • 34
  • 64
  • But what button do I link the Segue with in the storyboard? I obviously have to press ctrl + button and drag it to link a Segue in order to give it an id? – Omar Jul 18 '13 at 17:55
  • No need for a button -- just connect UIViewController to UIViewController. It's sometimes easiest to do the in the side bar that lists all your elements. – woz Jul 18 '13 at 17:57
1

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! :)

Community
  • 1
  • 1
leok
  • 31
  • 7