iosobjective-csegue

Creating a segue programmatically


I have a common UIViewController that all my UIViewsControllers extend to reuse some common operations.

I want to set up a segue on this "Common" UIViewController so that all the other UIViewControllers inherit.

I am trying to figure out how do I do that programmatically.

I guess that the question could also be how do I set a segue for all my UIViewControllers without going into the story board and do them by hand.


Solution

  • By definition a segue can't really exist independently of a storyboard. It's even there in the name of the class: UIStoryboardSegue. You don't create segues programmatically - it is the storyboard runtime that creates them for you. You can normally call performSegueWithIdentifier: in your view controller's code, but this relies on having a segue already set up in the storyboard to reference.

    What I think you are asking though is how you can create a method in your common view controller (base class) that will transition to a new view controller, and will be inherited by all derived classes. You could do this by creating a method like this one to your base class view controller:

    - (IBAction)pushMyNewViewController
    {
        MyNewViewController *myNewVC = [[MyNewViewController alloc] init];
    
        // do any setup you need for myNewVC
    
        [self presentModalViewController:myNewVC animated:YES];
    }
    

    and then in your derived class, call that method when the appropriate button is clicked or table row is selected or whatever.