从“segueForUnwindingToViewController()”移植到“unwindForSegue()”

Apple API文档指出segueForUnwindingToViewController()已被弃用,我应该使用unwindForSegue()来代替自定义的segue。 我发现这篇文章,但他们似乎仍然使用“segueForUnwindingToViewController()”。

我不知道如何正确使用unwindForSegue()因为segueForUnwindingToViewController()采取了以下参数

  • toViewController
  • fromViewController
  • 识别码

并返回了“UistoryBoardSegue”。 例如

  override func segueForUnwindingToViewController(toViewController: UIViewController, fromViewController: UIViewController, identifier: String?) -> UIStoryboardSegue? { return MyCustomSegue(identifier: identifier!, source: fromViewController, destination: toViewController, performHandler: { () -> Void in } ) } 

如何传递我的标识符,并使用新的unwindForSegue()创build自定义的segue实例?

那么,这似乎不是那么难。 我认为有很多方法来设置你的自定义赛格。

先决条件:

  1. 你需要创build自定义的segue类来执行UIViewControllers之间的转换。 不要忘记设置父类 – UistoryboardSegue 。 (例如CustomSegue)
  2. 你需要覆盖你的容器视图控制器中的方法-unwindForSegue:towardsViewController:你可以在代码中填充你的segue

重要的是 ,您不需要在不是容器的自定义视图控制器中重写此方法

一种方法 ,你可以select你在故事板中继续,并将其设置为你的segue类(例如CustomSegue

示例设置自定义segue类的位置

另一种方法 ,你可以从这个方法创buildsegue,并简单地执行它。 例如,

 - (void)unwindForSegue:(UIStoryboardSegue *)unwindSegue towardsViewController:(UIViewController *)subsequentVC { CustomSegue *segue = [[CustomSegue alloc] initWithIdentifier:<#Segue Identifier#> source:unwindSegue.sourceViewController destination:unwindSegue.destinationViewController]; [segue perform]; } 

两种方法都正常工作。 希望这可以帮助你。


PS你可以尝试这个testing项目(Xcode 7.3) – https://github.com/igorkislyuk/custom-animation-unwind-segue