Navigate to another ViewController programmatically using Custom Segue

12,491

Solution 1

Here's how I've done it. I create my segue subclass then call it from my sourceViewController IE the one I'm in, init my destinationViewcontroller then pass them to my custom segue class.

YourViewController *yourViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"YOURStoryboardViewControllerID"];
CustomSegueClass *segue = [[CustomSegueClass alloc] initWithIdentifier:@"ANYid" source:self destination:yourViewController];
[segue perform ];

Solution 2

I believe you have to use interface builder to inform the storyboard that a segue of a particular type should exist between two elements and how to identify it. You can trigger it in code using performSegueWithIdentifier: but, according to the docs, you can't actually create one in code:

"Your app never creates segue objects directly; they are always created on your behalf by iOS when a segue is triggered."

Solution 3

As Phillip Mills wrote, you have to create a segue in IB. You can do it by ctrl-dragging from one view controller to another and choosing "Custom" from the menu that appears. Then select the new segue and open attributes inspector. Enter an identifier for the segue and set its class to the name of the class you've implemented. After completing these steps you can use performSegueWithIdentifier:.

Solution 4

I'm assuming you're asking how you should implement the custom segue's subclass so that you don't have to use navigation controller?

If yes then I'd override the perform method in your LeftToRightSegue class as follows:

- (void)perform
{
  UIViewController *source = self.sourceViewController;
  UIViewController *destination = self.destinationViewController;

  UIWindow *window = source.view.window;

  CATransition *transition = [CATransition animation];
  [transition setDuration:1.0];
  [transition setTimingFunction:[CAMediaTimingFunction functionWithName:UIViewAnimationOptionCurveEaseInOut]];
  [transition setType:kCATransitionPush];
  [transition setSubtype:kCATransitionFromRight];
  [transition setFillMode:kCAFillModeForwards];
  [transition setRemovedOnCompletion:YES];


  [window.layer addAnimation:transition forKey:kCATransition];
  [window setRootViewController:self.destination];
}

You can change the type of animation if you want something different than a push. Also don't forget to import QuartzCore:

#import <QuartzCore/QuartzCore.h>

Solution 5

Swift way, Click on the Segue and select Custom. Now you can create a subclass of UIStoryboardSegue as follows

class MySegue: UIStoryboardSegue { override func perform() { let source = sourceViewController as! UIViewController if let navigation = source.navigationController { navigation.pushViewController(destinationViewController as! UIViewController, animated: false) } } }

then select Segue and set Class and module properly. Hope it helps

Share:
12,491

Related videos on Youtube

Ali
Author by

Ali

Updated on September 14, 2022

Comments

  • Ali
    Ali over 1 year

    I have two view controllers in the storyboard. I did successfully navigate from the 1st one to 2nd one programmatically:

    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    YourViewController *yourViewController = (YourViewController *)[storyboard instantiateViewControllerWithIdentifier:@"yourViewControllerID"];
    [self.navigationController pushViewController:yourViewController animated:YES];
    

    I want to use my custom Segue to navigate to other page instead of pushViewController without using interface builder.

    This is my custom Segue that I could easily use with interface builder:

    @interface LeftToRightSegue : UIStoryboardSegue
    

    How about do it programmatically?

  • Ali
    Ali over 11 years
    Thanks, but I think I wrote already in the question, that I have done these steps from IB.
  • Ali
    Ali over 11 years
    Thanks, What you assumed is wrong. my question is something else.
  • Ali
    Ali over 11 years
    Thanks, Alex. I'll try it and tell you the result.