Edvin
Edvin

Reputation: 2011

How to do the "Action segue modal" in code?

I am using Xcode 4.6 with storyboards.

In my storyboard I have a couple of UIViewControllers and with a button click I want to go from on ViewController to another. I have done this via adding a "Round Rect Button", control clicking it and dragging it to the ViewController I want to go to when the button is clicked and selected the "Action segue: modal".

This works perfectly fine! But I need to be able to do the same thing in code.

Thanks in advance for replies!

Upvotes: 0

Views: 866

Answers (1)

babygau
babygau

Reputation: 1581

Instead of dragging from button to destination VC, you make a new segue modal by Ctrl-dragging from source VC to destination VC, name the identifier. In your code, assume that you have an UIButton named mybtn. You can do the following step:

in viewDidLoad

[mybtn addTarget:self action:@selector(btnclicked:) forControlEvents:UIControlEventTouchUpInside];

in btnclicked method:

- (void)btnclicked:(UIButton *)sender {
    [self performSegueWithIdentifier:@"your_segue_identifier_name" sender:self];
}

finally, you can setup your data if you want to pass object between VC using the following delegate method. If you just simple navigate to destination VC, you could omit it:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqual:@"your_segue_identifier_name"]) {
        // Do your stuff here
    }
}

Upvotes: 1

Related Questions