xcode 使用自定义 Segue 以编程方式导航到另一个 ViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12992716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 01:53:08  来源:igfitidea点击:

Navigate to another ViewController programmatically using Custom Segue

iphoneobjective-ciosxcode

提问by Ali

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 customSegue to navigate to other page instead of pushViewControllerwithout using interface builder.

我想使用我的customSegue 导航到其他页面,而不是pushViewController不使用界面构建器。

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

这是我可以轻松与界面构建器一起使用的自定义 Segue:

@interface LeftToRightSegue : UIStoryboardSegue

How about do it programmatically?

如何以编程方式进行?

回答by Alex Reynolds

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.

这是我如何做到的。我创建了我的 segue 子类,然后从我所在的 sourceViewController IE 调用它,初始化我的 destinationViewcontroller,然后将它们传递给我的自定义 segue 类。

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

回答by Phillip Mills

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:

我相信您必须使用界面构建器来通知故事板在两个元素之间应该存在特定类型的转场以及如何识别它。您可以使用代码触发它,performSegueWithIdentifier:但根据文档,您实际上无法在代码中创建一个:

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

“您的应用程序从不直接创建 segue 对象;当 segue 被触发时,它们总是由 iOS 代表您创建。”

回答by Marcin Kuptel

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

正如 Phillip Mills 所写,你必须在 IB 中创建一个 segue。您可以通过按住 ctrl 从一个视图控制器拖动到另一个视图控制器并从出现的菜单中选择“自定义”来实现。然后选择新的 segue 并打开属性检查器。输入 segue 的标识符并将其类设置为您已实现的类的名称。完成这些步骤后,您可以使用performSegueWithIdentifier:.

回答by Sylvain Guillopé

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?

我假设您在问应该如何实现自定义 segue 的子类,以便您不必使用导航控制器?

If yes then I'd override the performmethod in your LeftToRightSegueclass as follows:

如果是,那么我将按如下perform方式覆盖您LeftToRightSegue班级中的方法:

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

如果你想要不同于推送的东西,你可以改变动画的类型。也不要忘记导入 QuartzCore:

#import <QuartzCore/QuartzCore.h>

回答by Vinu David Jose

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

Swift 方式,单击 Segue 并选择自定义。现在您可以创建 UIStoryboardSegue 的子类,如下所示

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

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

然后选择 Segue 并正确设置 Class 和 module。希望能帮助到你