1、兩個(gè)controller之間的跳轉(zhuǎn):A--彈出->B,B--回到->A
1)利用UINavigationController,調(diào)用pushViewController,進(jìn)行跳轉(zhuǎn);這種采用壓棧和出棧的方式,進(jìn)行Controller的管理。
調(diào)用popViewControllerAnimated方法可以返回。
跳轉(zhuǎn)視圖:
[self.navigationController popViewControllerAnimated:YES];
2)利用UIViewController自身的presentModalViewController,進(jìn)行跳轉(zhuǎn);
調(diào)用dismissModalViewControllerAnimated方法可以返回。
有個(gè)好處就是有個(gè)completion的block可用。
彈出視圖:
[self presentViewController:vc animated:YES completion:nil];
2、多個(gè)controller,跳轉(zhuǎn)到指定controller
1)使用UINavigationController,可以很方便的跳到指定的controller,navigationController提供了多個(gè)方法供大家使用。
定義一個(gè)屬性,用來持有要跳轉(zhuǎn)到指定的controller
@property(nonatomic, weak) id delegate;
//跳轉(zhuǎn)到指定的controller
[self.navigationController popToViewController:self.delegate animated:YES];
//跳轉(zhuǎn)到根controller
[self.navigationController popToRootViewControllerAnimated:YES];
//跳轉(zhuǎn)到前一個(gè)controller
[self.navigationController popViewControllerAnimated:YES];
使用navigationController要注意的是,保持navigationController的一致性,例如,A、B、C、D、E,依次彈出過程中,C自己重新創(chuàng)建了一個(gè)navigationController再彈出D、E,
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:C];
[B presentViewController:nav animated:YES completion:nil];
然后,E使用navigationController pop到A是會(huì)崩潰的,因?yàn)镃自己重新創(chuàng)建一個(gè)navigationController,就是重新創(chuàng)建了一個(gè)隊(duì)列,依次把D、E壓入棧中,A、B屬于另一個(gè)隊(duì)列,不能跨隊(duì)列跳轉(zhuǎn)自然就蹦了,在同一個(gè)隊(duì)列中是可以任意跳轉(zhuǎn)的。如下圖:

block的回調(diào):在跳轉(zhuǎn)controller的時(shí)候要調(diào)用block,是先調(diào)用block還是先pop controller呢?
對(duì)于像
B--回到->A相連controller,誰先誰后是沒有什么影響的,但是跨多個(gè)controller的話,就要考慮操作時(shí)間的問題了假如從E跳轉(zhuǎn)到A需要2秒,(A的block)block 1秒就執(zhí)行完,先調(diào)用block的話,block不一定會(huì)執(zhí)行成功。所以要先pop controller再執(zhí)行block,如上圖。
此外,navigationController也不能push navigationController,會(huì)崩潰的,如下圖。
