模态跳转和push跳转到公共页面如何返回

平时大家跳转页面,我们模态跳转到某个页面使用

[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];

    

}







  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值