新浦京81707con > 软件下载 > 你用对了吗,模态视图跳转

原标题:你用对了吗,模态视图跳转

浏览次数:134 时间:2020-04-21

在ViewController中:

presentingViewController

The view controller that presented this view controller.When you present a view controller modally (either explicitly or implicitly) using the presentViewController:animated:completion: method, the view controller that was presented has this property set to the view controller that presented it. If the view controller was not presented modally, but one of its ancestors was, this property contains the view controller that presented the ancestor. If neither the current view controller or any of its ancestors were presented modally, the value in this property is nil.

大意是:展现此视图调整器的视图调节器。当你使用presentViewController:animated:completion: 方法表现三个视图调节器时,展现的视图调控器将那个性情设置为表现它的视图控制器。如若视图调整器不是以模态显示的,但是它的上顶级是,这么些性情包罗呈现上顶尖的视图调节器。如若当前视图调节器或它的其它上拔尖都不曾以模态格局浮现,则此属性中的值为nil。

1. 有的表明

图片 1dimiss_process.png

例如表明:

有四个调节器AB,A通过present模态推出B,那么A的presentedViewController就是B,也等于您通过present模态推出了什么人,你的presentedViewController正是什么人,那样就好通晓了,哈哈,那天性子就是这么简单,当然,如果没有模态推出任何调整器,那么属性值就是nil

嗯,剩下叁个dismissViewControllerAnimated那个格局解释完,就不会愧对自身的标题了,那几个法子有两种选拔方式啊,跟着本人继续看吧......

3. 两个模态视图之间跳转

回去我们前言的需求,怎么着从C视图调节器直接回到到A视图调节器?

在网络也看出局地解答,举例说使用文告让B视图调整器执行dismissViewController办法。其实那样是非常的,原因刚刚已经表达过,对于多个视图调整器X,它实践dismissViewController格局的时候将会停业它present的模态视图,唯有在它从未present过模态视图的时候(不过此间B已经present到C了),才会付给她的presentingViewController执行dismissViewController艺术。所以这里若是交给B实践,和一直在C里面施行dismissViewController办法的作用是同一的。

明圣元个最简易的扫除办法即是行使公告或许代理,在A中施行dismissViewController主意。当时B和C视图调节器会发生什么变动吗?

照例摘录一段苹果的文档做一下解释:

If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

相当于说,其实际present七个视图调整器的时候,系统珍爱了三个栈,以我们明日以此情况为例,从栈底到栈顶依次是A->B->C。当栈中某些地点的视图调节器施行dismissViewController方法的时候,栈中全部在它之上的视图调控器都会被dismiss,差别的是,栈顶的视图调整器将会以卡通方式被dismiss,而中级的视图调整器只是简短的remove掉。

这种办法是有效的,因为 dismissViewController连天要在A方法中奉行的。也才那样做会遭受代码耦合的难题。

一种低耦合的减轻方案:

// C视图控制器触发dismiss方法前添加这么一段代码
UIViewController *rootVC = self.presentingViewController;
// rootVC.view.alpha = 0; 
while (rootVC.presentingViewController) {
    rootVC = rootVC.presentingViewController;
}
[rootVC dismissViewControllerAnimated:YES completion:nil];

在循环中三番若干次收获presentingViewController,于是最终得以获取根视图调控器,这里正是A,但是那使得A视图调节器中不要增添任何代码,进而缓和了耦合的主题材料。

如此那般写的另二个益处是,不管是多少个视图调整器之间的跳转,都足以很有益于的酿成。

劣点:你会分明的以为到是从C->B->A,有未有一种完美的缓慢解决方案,让客户认为直接从C->A?

那句代码rootVC.view.alpha = 0;的步入能够让达到C->A的作用,可是Animated的安装就不行了。

恐怕求完美的解决办法。

图片 2

A->B->C.gif

能够简轻松单归纳为两点:

抑或先看文书档案解释

Dismisses the view controller that was presented modally by the view controller.The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.If you want to retain a reference to the view controller'��s presented view controller, get the value in the presentedViewControllerproperty before calling this method.The completion handler is called after the viewDidDisappear: method is called on the presented view controller.

马虎是【这里自身就无须翻译软件翻译了,翻译的都相当不够标准,直接是小编要好的接头,大家感觉有哪些不标准的应接建议】:这一个法子是用来将某三个透过present模态呈现的视图调控器撤销掉模态呈现设若老是模态推出了四个调控器,并在最上超级调节器实行了该方法,则会从最上顶尖调节器开头,前面全体通过present呈现的调整器都会被dismiss掉,且这几个调控器在被dismiss的时候照旧会进行viewWillAppear以至viewDidDisappear等相关视图彰显或消除的法子,不过未有的进程却是一会儿的,不论中间有些个调节器,你看看的效能就好像只dismiss了三个调节器相仿

比方表达:有5个调整器ABCDE,应用运行后出示根调整器A,之后Apresent出B,B present出C ,,,,一贯到E,那么等到E完全展现的这时候,若是A试行了dismiss,你将寻访到B消失的卡通片,同一时间,CDE也会被dismiss,实际上dismiss的依次和你present的次第一致,即从E->D->C->B,可是独有B才会有dismiss的出产动漫,想必是苹果对这一局地做了优化

跻身正题步入正题步向正题步向正题走入正题步入正题进入正题步向正题

如今谈到了dismissViewControllerAnimated有三种用法,其实大家从文书档案中就应该开采这么些方法是用来撤销掉某一个调整器的模态展现的,那么是何人来撤除掉的呢?日常大家都是如此写的啊,如下:[self dismissViewControllerAnimated:YES completion:nil];如此写的意义是:让系统【即UIKIT框架之中管理】将如今决定器dismiss掉

但实在,我们理应这么写:if (self.presentingViewController) { [self.presentingViewController dismissViewControllerAnimated:YES completion:nil]; } else { [self dismissViewControllerAnimated:YES completion:nil]; }那般写的含义是:当前展现的调节器是由什么人模态推出的就由何人来dismiss掉,假诺是最上拔尖的就交付系统来dismiss,因为最上一层未有presentingViewController,为什么没有,我们再回到对照presentingViewController的概念,哈哈哈哈,看看B的presentingViewController是还是不是从未有过

终极便是使用途景了:

假使八个调节器都通过 present 的议程跳转呢?例如从A跳转到B,从B跳转到C,从C跳转到D,如何由D直接回到到A呢?可以因此presentingViewController 平素找到A调节器,然后调用A调控器的 dismissViewControllerAnimated 方法。方法如下:UIViewController *controller = self; while(controller.presentingViewController != nil){ controller = controller.presentingViewController; } [controller dismissViewControllerAnimated:YES completion:nil];PS:假如不是想一贯回到到A调控器,譬如想回去B调整器,while循环的告一段落条件得以由此调整器的类来判断。

OK,打完收工,装逼要有限度

1.1 几个常用的方法

/**
 *  展示模态视图
 *
 *  @param viewControllerToPresent 调转的目标控制器
 *  @param flag                    如果flag == NO,那么不会执行任何动画(当然也不会执行自定义动画);如果flag == YES,则有可能执行自定义动画,如果没有自定义动画则会执行系统默认动画。
 *  @param completion              跳转完成后的回调
 */
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion;

/**
 *  关闭模态视图
 *
 *  @param flag       如果flag == NO,那么不会执行任何动画(当然也不会执行自定义动画);如果flag == YES,则有可能执行自定义动画,如果没有自定义动画则会执行系统默认动画。
 *  @param completion 关闭完成后的回调
 */
- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion;

在AViewController中执行disimiss:

不时候在二遍想要判定当前调节器是由此present模态推出依然经过push入栈呈现的时候注意到了presentingViewController和presentedViewController两脾性子,即:要是presentingViewController不为空,则当前调节器是经过present模态推出的不二秘籍显示的,那么相应的回到格局应该是dismiss并不是pop,之后又在头文件见到还有一个presentedViewController,就钻探了下那七个属性的切实意思

5. References

  • dismissViewController达成多少个模态视图跳转

Dismisses the view controller that was presented modally by the view controller.The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, UIKit asks the presenting view controller to handle the dismissal.If you present several view controllers in succession, thus building a stack of presented view controllers, calling this method on a view controller lower in the stack dismisses its immediate child view controller and all view controllers above that child on the stack. When this happens, only the top-most view is dismissed in an animated fashion; any intermediate view controllers are simply removed from the stack. The top-most view is dismissed using its modal transition style, which may differ from the styles used by other view controllers lower in the stack.

presentedViewController

The view controller that is presented by this view controller, or one of its ancestors in the view controller hierarchy.When you present a view controller modally (either explicitly or implicitly) using the presentViewController:animated:completion: method, the view controller that called the method has this property set to the view controller that it presented. If the current view controller did not present another view controller modally, the value in this property is nil.

忽略是:视图调节器由该视图调节器展现,或其在视图调整器档次构造中的祖先之一。当你使用presentViewController:animated:completion: method方法以模态格局表现贰个视图调节器时,调用该措施的视图调控器将以此特性设置为它所展现的视图调整器。假诺当前视图调控器未有以情势展现另一个视图控制器,则此属性中的值为nil。

4. GitHub

  • PresentAndDismiss

透过上边的文书档案介绍,我们得以看来在本文刚伊始介绍的最简易的使用处境下(ViewController present AViewController),在AViewController中执行[self dismissViewControllerAnimated:YES completion:nil]和在ViewController中执行[self dismissViewControllerAnimated:YES completion:nil]职能是均等的,那或多或少是因为系统帮大家处理好了(因为系统判判AViewController当前从未有过present出来任何调控器,所以系统会找到它的presentingViewController,也便是ViewController来实践dismiss事件)。在错综相连一点的场所下,举例大家要dismiss掉当前被present出来的调节器的话,大家就必要想艺术取得地处栈底的可怜调整器,在这里个调控器中推行[self dismissViewControllerAnimated:YES completion:nil]才行。

举例表明:

有多个调控器 ABCDE,应用运转先出示根调整器A,之后A通过present情势生产带导航栏的B,B再通过push的主意推出C,C再经过push的秘诀推出D,D再通过push的章程推出E,那么E的 presentingViewController 便是B也便是presentingViewController即为当前调控器的顶头上司调控器,并且是因此present格局体现的父级,下边例子中独有B是经过present方式临蓐的,且B是CDE的父级,那么 D 的presentingViewController也将是B,大家能够协和知道一下同理:如若依然有七个调控器 ABCDE,应用运营先出示根调节器A,之后全体是透过present方式挨个推出 B、C、D、E,那么,那个时候E的presentingViewController便是D了,D的presentingViewController正是C

哈哈,会不会说的有个别多,其实本人看看概念渐渐想就能想通的,后边会再介绍一下这么些性格有何样用,会用在哪一种现象

必要:A视图调控器中presentB视图调整器,B视图调节器再presentC视图调节器。最终从C视图调整器直接回到到A视图调节器。

咱俩先看看官方文书档案到底怎么讲的:

2. dismissViewController在哪个地方实施?

大家最常使用的一种景况:A视图调节器present跳转到B视图调整器,B视图调整器dismiss再次回到A视图调控器,所以会想当然的以为dismissViewController这么些措施是在B视图调节器里面施行。

标准答案是:在A视图调整器里举行。

苹果文书档案有诸有此类一段话:

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.

也许我们假诺记住一个简约的法则:

谁污染,谁治理!

可是,在上述场景中,用B视图调控器实行也是足以的,因为系统会活动优化,当B视图调节器未有present过其它视图调整器的时候,dismissViewController方法会自动提交B视图调节器的presentingViewController奉行,也等于A视图调控器。

UIViewController *presentingVc = self.presentingViewController;while (presentingVc.presentingViewController) { presentingVc = vc.presentingViewController;}if(presentingVc){ [presentingVc dismissViewControllerAnimated:YES completion:nil];}

1.2 七个名词表达

// The view controller that was presented by this view controller or its nearest ancestor.
@property(nullable, nonatomic,readonly) UIViewController *presentedViewController  NS_AVAILABLE_IOS(5_0);

// The view controller that presented this view controller (or its farthest ancestor.)
@property(nullable, nonatomic,readonly) UIViewController *presentingViewController NS_AVAILABLE_IOS(5_0);

ApresentB,A是presentedViewController,B是presentingViewController。

本文由新浦京81707con发布于软件下载,转载请注明出处:你用对了吗,模态视图跳转

关键词: 新浦京81707con 你用 对了

上一篇:开发者讲讲函数式编程,函数式编程

下一篇:没有了