平時(shí)大家跳轉(zhuǎn)頁(yè)面,我們模態(tài)跳轉(zhuǎn)到某個(gè)頁(yè)面使用
[selfpresentViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>]
返回時(shí)使用對(duì)應(yīng)的返回方法
[selfdismissViewControllerAnimated:YEScompletion:nil];
push跳轉(zhuǎn)頁(yè)面使用
[self.navigationController pushViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#>]
對(duì)應(yīng)的返回方法為
[self.navigationControllerpopViewControllerAnimated:YES];
這里提出一個(gè)場(chǎng)景:從頁(yè)面A可以模態(tài)跳轉(zhuǎn)到公共頁(yè)面M,從頁(yè)面B也可以push跳轉(zhuǎn)到頁(yè)面M,那么頁(yè)面M相應(yīng)的返回按鈕應(yīng)該怎么實(shí)現(xiàn),這里我給出一個(gè)解決方案:
給公共頁(yè)面M聲明一個(gè)屬性:
@property(nonatomic,assign)BOOLmark;
//a.從頁(yè)面A跳轉(zhuǎn)到公共頁(yè)面M
M* mainDetailVC = [[Malloc]init];
mainDetailVC.mark=YES;
UINavigationController*navc = [[UINavigationControlleralloc]initWithRootViewController:mainDetailVC];
[selfpresentViewController:navcanimated:YEScompletion:nil];
//b.從頁(yè)面B跳轉(zhuǎn)到公共頁(yè)面M
M* mainDetailVC = [[Malloc]init];
[self.navigationControllerpushViewController:mainDetailVCanimated:NO];
最后一步:在控制器M中實(shí)現(xiàn)返回按鈕,需要兩種返回方法
- (void)viewWillAppear:(BOOL)animated {
//判斷是模態(tài)過(guò)來(lái)的還是push過(guò)來(lái)的,對(duì)應(yīng)創(chuàng)建相應(yīng)的返回按鈕及方法
if(self.mark==YES) {
UIBarButtonItem*btn = [[UIBarButtonItemalloc]initWithTitle:@"返回"style:(UIBarButtonItemStylePlain)target:selfaction:@selector(clickback:)];
self.navigationItem.leftBarButtonItem= btn;
}
else{UIBarButtonItem*btn = [[UIBarButtonItemalloc]initWithTitle:@"返回"style:(UIBarButtonItemStylePlain)target:selfaction:@selector(clickbacktwo:)];
self.navigationItem.leftBarButtonItem= btn;
}
}
//模態(tài)返回方法,如果是模態(tài)過(guò)來(lái)的就會(huì)使用此方法返回
- (void)clickback:(UIBarButtonItem*)but {
[selfdismissViewControllerAnimated:YEScompletion:nil];
}
//pop返回方法,如果是push過(guò)來(lái)的就會(huì)使用此方法
- (void)clickbacktwo:(UIBarButtonItem*)but {
[self.navigationControllerpopViewControllerAnimated:YES];
}