Make button it UIAlertView perform Segue

时光总嘲笑我的痴心妄想 提交于 2019-11-30 09:17:33

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

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.

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

    }
}
leok

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

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