iOS——自定义Segue实现总结
在阅读了iOS 8自定义动画转场上手指南后,刚开始不理解,后来慢慢消化了,现在总结如下:
1. 自定义Segue关键在于继承UIStoryboardSegue并重写perform方法
2. 动画可以在转场之间的任何方法中添加(Segue的perform方法、prepareSegue、UnwindSegue对应的IBAction方法),但最好统一一下,放在perform中去
3. UnwindSegue关键要在返回的目标VC中定义IBAction,然后才能在Storyboard中通过拖拽并选择IBAction来创建UnwindSegue
4. 创建Segue的时候一定记得要写SegueId(Storyboard中选中Segue后右侧属性列表中有),代码中需要通过id来判断segue
5. -(UIStoryboardSegue *)segueForUnwindingToViewController:(UIViewController *)toViewController fromViewController:(UIViewController *)fromViewController identifier:(NSString *)identifier 一定要在转场目标VC中重写此方法,而不是源VC中
6. 转场动画结束后,记得调用
self.sourceViewController.presentViewController(self.destinationViewController as! UIViewController, animated: false, completion: nil)
或者
self.sourceViewController.dismissViewControllerAnimated(false, completion: nil)
来最终完成segue工作。
7. Segue转场流程:[A performSegueWithIdentifier:sender:] --> [B init] [B awakeFromNib]--> Find segue defined in storyboard by id. --> [A prepareForSegue:sender:](传递数据) --> [customSegue perform] --> [B viewDidload]
UnwindSegue转场流程:[B performSegueWithIdentifier:sender: --> [A segueForUnwindingToViewController:fromViewController:identifier:](Find unwind segue from destination VC's segueForUnwindingToViewController selector.) --> [B prepareForSegue:sender:](传递数据) --> [A -(IBAction)returnFromSegue:(UIStoryboardSegue *)sender] (IBAction relative by unwind segue id which defined in storyboard)
搞定,希望能帮助到有需要的猿!