当前位置: 动力学知识库 > 问答 > 编程问答 >

iphone - 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?

网友答案:

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 ];
网友答案:

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

网友答案:

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

网友答案:

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>
网友答案:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: