您的位置:首页 > 健康 > 美食 > 长沙模板建站欢迎咨询_丹阳疫情最新数据_太原seo网站排名_佛山网站排名提升

长沙模板建站欢迎咨询_丹阳疫情最新数据_太原seo网站排名_佛山网站排名提升

2024/10/6 8:25:35 来源:https://blog.csdn.net/2301_80253909/article/details/142319506  浏览:    关键词:长沙模板建站欢迎咨询_丹阳疫情最新数据_太原seo网站排名_佛山网站排名提升
长沙模板建站欢迎咨询_丹阳疫情最新数据_太原seo网站排名_佛山网站排名提升

iOS学习

  • 前言
  • push与pop
  • present与dismiss
  • 使用dismiss弹出多级
    • PresentedViewController 与 PresentingViewController区别
  • 总结

前言

在此前就学习过视图的push与present。与之对应的退出方法为pop与dismiss。这里进行一次总结。


push与pop

pushViewController 是通过导航控制器入栈的方式切换页面
方法使用为先创建一个视图,后push进栈:

    secondViewController *secondVC = [[secondViewController alloc] init];[self.navigationController pushViewController:secondVC animated:YES];

pop则是与之对应的弹出视图的方法,具体使用如下:

	//返回上一级[self.navigationController popViewControllerAnimated:YES];//返回根视图[self.navigationController popToRootViewControllerAnimated:YES];//返回指定级数 (objectAtIndex:参数为想要返回的级数)[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0]  animated:YES];

present与dismiss

presentViewController 是通过模态切换的方式切换页面
具体使用方法如下:

    secont_presentViewController *second_Pre = [[secont_presentViewController alloc] init];[self presentViewController:second_Pre  animated:YES completion:nil];

与push不同,present不需要使用导航控制器。因此不将视图压入导航控制器也可以推出。

对应的弹出视图的方法为dismiss

    [self dismissViewControllerAnimated:YES completion:nil];

使用dismiss弹出多级

PresentedViewController 与 PresentingViewController区别

PresentedViewController和PresentingViewController是UIViewController中的两个属性。

  • presentedViewController:The view controller that was presented by
    this view controller or its nearest ancestor. 由这个视图控制器或它最近的祖先呈现的视图控制器
  • presentingViewController:The view controller that presented this view
    controller (or its farthest ancestor.) 呈现此视图控制器(或其最远祖先)的视图控制器。

当我们对视图A使用present推出视图B时。
A.presentedViewController 就是B控制器;
B.presentingViewController 就是A控制器;

由此,我们可以通过判断presentingViewController属性是否存在,来跳转到根视图

-(void)btn_dismissToRoot
{UIViewController *rootVC = self.presentingViewController;while  (rootVC.presentingViewController ) {rootVC = rootVC.presentingViewController ;}[rootVC dismissViewControllerAnimated:YES completion:nil];
}

也可以直接dismiss到对应的presentingViewController属性的层级,来跳转到我们需要的视图中
如,跳转到上上个视图

    [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];

或者通过判断是否为对应子类

-(void)btn_dismissTosecondTwo
{UIViewController *rootVC =  self.presentingViewController;while (![rootVC isKindOfClass:[secont_presentViewController class]])  {rootVC = rootVC.presentingViewController;}[rootVC dismissViewControllerAnimated:YES completion:nil];
}

实现效果:
请添加图片描述

总结

使用业务逻辑不同,界面推出的方式也不同。
present用于不同业务界面的切换。push用于同一业务不同界面之间的切换。
还值得我们注意的是,当我们present进入一个界面后,是不能push推出下一个界面的。这点会造成界面推出后无法收回,或者无法推出下一个界面的bug,以后遇到了再详细研究吧。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com