Unwind Segue,可以方便的在Navigation Controller的Controllers之间回退–返回至前若干个Controller,简单的说就是控制器之间的跳转,同时可以传值,省去了过去的Deleagte等跳转传值方法的步骤。
例如,Navigation Controller栈中有五个Controller:A、B、C、D、E,其中A push B,B push C,C push D,一直不断的push,这时我想退回,有选择的退回。如我想从 E退回至A 或 B退回至A 或 E退回至A,可以方便的使用Unwind Segue实现。
下面我做了,Root0->Green1->Yellow2->Orange3这样的跳转(跟着一起做吧)
1) Orange3视图可以通过unwind segue方便的返回到Root0,不传值
2) Green1视图可以通过unwind segue方便的返回到Root0 ,同时传值回去
Demo地址:https://github.com/BigBagFind/UnwindDemo
1.新建如下控制器,Root0push到Green1,Green1Push到Yellow2,Yellow2Push到Orange3,

2.打开需要返回到的界面,我这里是第一个Root0控制器,即RootViewController.m,加上下面方法,返回类型一定是IBAction,参数类型一定是UIStoryboardSegue
下面的我写的方法:
- (IBAction)ViewControllerUnwindSegue:(UIStoryboardSegue *)segue{
}
3.在storyboard里找到要跳转的界面进行连线,这里我在OrangeVC,做如下操作
(1)右键Orange上面的第三个按钮,会出现第一个RootVC写的IBAction方法(RootVC没写的话当然就是空的!)

(2)左键方法右边的加号拉到back按钮上面,添加出发事件的对象

(3)左键点击action确定
(4)这时候你点击OrangeVC3的back即可跳回到RootVC0

4.单纯的跳转就是如上面的1、2、3操作,这里开始在跳转中进行传值,就是利用storyBoardSegue进行传值,这里我用GreenVC1跳转到RootVC0同时传如一个text字符串
(1)来到GreenVC,右键Exit,左键“+”号拉到GreenVC本身,就是说GreenVC来触发这个事件

(2)拉线之后出现manual,然后左键点击即可

(3)这时左边GreenVC出现了Unwind,点击设置identifier

(4)在GreenVC中为segue设置要传的值
- (void)prepareForSegue:(UIStoryboardSegue*)segue sender:(id)sender{
if([segue.identifier isEqualToString:@"from2To1"]) {
RootViewController*destVc = segue.destinationViewController;
destVc.text=@"我传过来咯";
}
}
(5)设置GreenVC中Back按钮的Action
- (IBAction)back:(id)sender {
[self performSegueWithIdentifier:@"from2To1" sender:nil];
}
(6) 在Root0 自己写的IBAction方法中接受传值并且log
if([segue.identifierisEqualToString:@"from2To1"]) {
NSLog(@"back\n");
NSLog(@"\t%@",self.text);
}

网友评论