我只是知道 self.presentViewController(childVc, animated: true, completion: {})
中的完成 block 在 subview Controller 完成动画后被调用 显示在屏幕上。实际上,我想运行一段代码,在 subview Controller 的动画完成后显示关闭。我该怎么做,最好避免使用委托(delegate)来做到这一点?
编辑:我从呈现的(子) View Controller 中这样调用它:self.presentingViewController.dismissViewControllerAnimated (true) {}
,但问题是 subview Controller 可以从任意页面,在 child 被解雇后,父(呈现) View Controller 必须做不同的事情。如果我在 child 中实现回调,我不能对调用它的不同 parent 做不同的事情。
例如,如果我在新闻屏幕中调用登录表单作为模式,在登录表单关闭后,我想随后显示评论部分。但是,如果登录表单以模式方式从产品屏幕显示,我想随后在购物车中显示用户的购物车和产品。如果没有委托(delegate)就无法完成,我仍然对委托(delegate)解决方案感兴趣。
最佳答案
您可以向您的 ChildVC
添加一个属性来存储回调闭包,并在您呈现 subview Controller 时设置它。当 subview Controller 自行关闭时,它可以调用完成处理程序:
class ChildVC: UIViewController {
var completionHandler : ((childVC:ChildVC) -> Void)?
func dismiss() {
self.dismissViewControllerAnimated(true) {
self.completionHandler?(childVC:self)
}
}
}
在呈现 ChildVC 实例时提供完成处理程序:
let completionHandler:(ChildVC)->Void = { childVC in
print("completed for \(childVC)")
}
childVC.completionHandler=completionHandler
self.presentViewController(childVC, animated: true, completion: nil)
关于ios - 如何模态呈现 ViewController,然后在 ViewController 被关闭后运行回调函数/ block ,没有委托(delegate)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37558473/