关闭呈现的视图控制器 [英] Dismissing a Presented View Controller

查看:34
本文介绍了关闭呈现的视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个理论问题.现在我正在阅读 Apple 的 ViewController 指南.

I have a theoretic question. Now İ'm reading Apple's ViewController guide.

他们写道:

当需要关闭呈现的视图控制器时,首选方法是让呈现视图控制器关闭它.换句话说,只要有可能,同一个视图控制器呈现的视图控制器也应该负责驳回它.尽管有多种技术可以通知呈现其呈现的视图控制器应该的视图控制器被驳回,首选技术是委托.

When it comes time to dismiss a presented view controller, the preferred approach is to let the presenting view controller dismiss it. In other words, whenever possible, the same view controller that presented the view controller should also take responsibility for dismissing it. Although there are several techniques for notifying the presenting view controller that its presented view controller should be dismissed, the preferred technique is delegation.

但我无法解释,为什么我必须在呈现的 VC 中创建一个协议并添加委托变量,在呈现 VC 中创建委托方法以消除呈现的 VC,而不是在 呈现 中的简单调用视图控制器方法

But I can't explain, why I have to create a protocol in presented VC and add delegate varible, create delegate method in presenting VC for dismissing presented VC, instead of a simple call in presented view controller method

[self deniedViewControllerAnimated:NO completion:nil]?

为什么首选更好?为什么苹果推荐它?

Why is the first choice better? Why does Apple recommend it?

推荐答案

我认为 Apple 在这方面有点掩饰自己的潜在 API 部分.

I think Apple are covering their backs a little here for a potentially kludgy piece of API.

  [self dismissViewControllerAnimated:NO completion:nil]

实际上有点像小提琴.尽管您可以 - 合法地 - 在呈现的视图控制器上调用它,但它所做的只是将消息转发到呈现的视图控制器.如果你想在解雇 VC 之外做任何事情,你需要知道这一点,你需要像对待委托方法一样对待它 - 因为它几乎就是这样,一个有点不灵活的烘焙委托方法.

Is actually a bit of a fiddle. Although you can - legitimately - call this on the presented view controller, all it does is forward the message on to the presenting view controller. If you want to do anything over and above just dismissing the VC, you will need to know this, and you need to treat it much the same way as a delegate method - as that's pretty much what it is, a baked-in somewhat inflexible delegate method.

也许他们遇到了很多糟糕的代码,因为人们并不真正理解这些代码是如何组合在一起的,因此他们很谨慎.

Perhaps they've come across loads of bad code by people not really understanding how this is put together, hence their caution.

当然,如果您需要做的只是忽略这件事,请继续.

But of course, if all you need to do is dismiss the thing, go ahead.

我自己的做法是一种妥协,至少它提醒了我发生了什么:

My own approach is a compromise, at least it reminds me what is going on:

  [[self presentingViewController] dismissViewControllerAnimated:NO completion:nil]

[Swift]

  self.presentingViewController?.dismiss(animated: false, completion:nil)

这篇关于关闭呈现的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆