2

我在情节提要中有两个视图控制器。我确实以编程方式成功地从第一个导航到第二个:

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

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

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

@interface LeftToRightSegue : UIStoryboardSegue

如何以编程方式进行?

4

5 回答 5

8

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

YourViewController *yourViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"YOURStoryboardViewControllerID"];
CustomSegueClass *segue = [[CustomSegueClass alloc] initWithIdentifier:@"ANYid" source:self destination:yourViewController];
[segue perform ];
于 2012-10-25T22:04:05.247 回答
3

我相信您必须使用界面构建器来通知情节提要,两个元素之间应该存在特定类型的 segue 以及如何识别它。您可以使用代码在代码中触发它,performSegueWithIdentifier:但是根据文档,您实际上不能在代码中创建一个:

“你的应用永远不会直接创建 segue 对象;它们总是在触发 segue 时由 iOS 代表你创建。”

于 2012-10-20T21:59:28.733 回答
2

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

于 2012-10-20T22:22:13.110 回答
1

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

如果是,那么我将覆盖performLeftToRightSegue类中的方法,如下所示:

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

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

#import <QuartzCore/QuartzCore.h>
于 2012-10-20T22:33:35.747 回答
1

快速方式,单击 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) } } }

然后选择 Segue 并正确设置类和模块。希望能帮助到你

于 2015-11-25T09:36:22.810 回答