[self presentViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>]
返回时使用对应的返回方法
[self dismissViewControllerAnimated:YES completion:nil];
push跳转页面使用
[self.navigationController pushViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#>]
对应的返回方法为
[self.navigationController popViewControllerAnimated:YES];
这里提出一个场景:从页面A可以模态跳转到公共页面M,从页面B也可以push跳转到页面M,那么页面M相应的返回按钮应该怎么实现,这里我给出一个解决方案:
给公共页面M声明一个属性:
@property (nonatomic, assign) BOOL mark;
//a.从页面A跳转到公共页面M
M * mainDetailVC = [[M alloc]init];
mainDetailVC.mark = YES;
UINavigationController *navc = [[UINavigationController alloc]initWithRootViewController:mainDetailVC];
[self presentViewController:navc animated:YES completion:nil];
//b.从页面B跳转到公共页面M
M * mainDetailVC = [[M alloc]init];
[self.navigationController pushViewController:mainDetailVC animated:NO];
最后一步:在控制器M中实现返回按钮,需要两种返回方法
- (void)viewWillAppear:(BOOL)animated {
//判断是模态过来的还是push过来的,对应创建相应的返回按钮及方法
if (self.mark == YES) {
UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(clickback:)];
self.navigationItem.leftBarButtonItem = btn;
}
else { UIBarButtonItem *btn = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:(UIBarButtonItemStylePlain) target:self action:@selector(clickbacktwo:)];
self.navigationItem.leftBarButtonItem = btn;
}
}
//模态返回方法,如果是模态过来的就会使用此方法返回
- (void)clickback:(UIBarButtonItem *)but {
[self dismissViewControllerAnimated:YES completion:nil];
}
//pop返回方法,如果是push过来的就会使用此方法
- (void)clickbacktwo:(UIBarButtonItem *)but {
[self.navigationController popViewControllerAnimated:YES];
}