平时大家跳转页面,我们模态跳转到某个页面使用
[selfpresentViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>]
返回时使用对应的返回方法
[selfdismissViewControllerAnimated:YEScompletion:nil];
push跳转页面使用
[self.navigationController pushViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#>]
对应的返回方法为
[self.navigationControllerpopViewControllerAnimated:YES];
这里提出一个场景:从页面A可以模态跳转到公共页面M,从页面B也可以push跳转到页面M,那么页面M相应的返回按钮应该怎么实现,这里我给出一个解决方案:
给公共页面M声明一个属性:
@property(nonatomic,assign)BOOLmark;
//a.从页面A跳转到公共页面M
M* mainDetailVC = [[Malloc]init];
mainDetailVC.mark=YES;
UINavigationController*navc = [[UINavigationControlleralloc]initWithRootViewController:mainDetailVC];
[selfpresentViewController:navcanimated:YEScompletion:nil];
//b.从页面B跳转到公共页面M
M* mainDetailVC = [[Malloc]init];
[self.navigationControllerpushViewController:mainDetailVCanimated:NO];
最后一步:在控制器M中实现返回按钮,需要两种返回方法
- (void)viewWillAppear:(BOOL)animated {
//判断是模态过来的还是push过来的,对应创建相应的返回按钮及方法
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;
}
}
//模态返回方法,如果是模态过来的就会使用此方法返回
- (void)clickback:(UIBarButtonItem*)but {
[selfdismissViewControllerAnimated:YEScompletion:nil];
}
//pop返回方法,如果是push过来的就会使用此方法
- (void)clickbacktwo:(UIBarButtonItem*)but {
[self.navigationControllerpopViewControllerAnimated:YES];
}
网友评论