Ali
Ali

Reputation: 9994

Navigate to another ViewController programmatically using Custom Segue

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?

Upvotes: 2

Views: 12533

Answers (5)

Vinu David Jose
Vinu David Jose

Reputation: 2638

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

Upvotes: 1

Alex Reynolds
Alex Reynolds

Reputation: 6342

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

Upvotes: 8

Marcin Kuptel
Marcin Kuptel

Reputation: 2664

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

Upvotes: 2

Sylvain Guillopé
Sylvain Guillopé

Reputation: 1367

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>

Upvotes: 1

Phillip Mills
Phillip Mills

Reputation: 31016

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

Upvotes: 3

Related Questions