iphone - 关闭呈现的 View Controller

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

他们写道:

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 中创建协议(protocol)并添加委托(delegate)变量,在呈现 VC 中创建委托(delegate)方法以关闭呈现的 VC,而不是在 presented 中进行简单调用 View Controller 方法

[self dismissViewControllerAnimated:NO completion:nil]?

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

最佳答案

我认为 Apple 在这里稍微掩饰了一些潜在的笨拙 API。

  [self dismissViewControllerAnimated:NO completion:nil]

其实有点小题大做。尽管您可以合法地在呈现的 View Controller 上调用它,但它所做的只是将消息转发到呈现的 View Controller 。如果您想在关闭 VC 之外做任何事情,您将需要知道这一点,并且您需要以与委托(delegate)方法相同的方式对待它 - 因为这几乎就是它的本质,一个烘焙的有点不灵活委托(delegate)方法。

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

当然,如果您需要做的就是忽略这件事,那就继续吧。

我自己的做法是一种妥协,至少它提醒了我是怎么回事:

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

[ swift ]

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

关于iphone - 关闭呈现的 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14636891/

相关文章:

objective-c - iOS8 - 约束含糊地暗示高度为零

ios - UICollectionView 当前可见单元格索引

ios - 检查 AVPlayer 的播放状态

objective-c - iOS 7.0.3 上的 "HelveticaNeue-Italic"发

ios - 设置 UILabel 行距

ios - 在单行 UILabel 旁边居中 NSTextAttachment 图像

ios - 对 UITableView 使用 didSelectRowAtIndexPath 或 p

ios - 带有透明孔的 CALayer

ios - "From View Controller"使用 UIViewControllerCon

ios - UICollectionViewCell 上的长按手势