There is a chapter on how showViewController:sender:
and showDetailViewController:sender:
work in Programming iOS 8: Dive Deep into Views, View Controllers, and Frameworks.
When these methods are called, they call targetViewControllerForAction:sender:
on themselves and call this method on the returned object. The target object can then display the view controller in an appropriate way. For example, a navigation controller pushes the view controller on its navigation stack.
So you can create a generic dismissVC:
method and override it in different UIViewController
subclasses.
extension UIViewController {
func dismissVC(sender:AnyObject?) {
if let presentingVC = targetViewControllerForAction("dismissVC", withSender: sender) as? UIViewController {
presentingVC.dismissVC(self)
}
}
}
extension UINavigationController {
override func dismissVC(sender: AnyObject?) {
popViewControllerAnimated(true)
}
}
extension CrazyCustomContainer {
override func dismissVC(sender: AnyObject?) {
someWackyUnwindMethod()
}
}
This way, when you call dismissVC:
method, if will always correctly dismiss the view controller depending on the context.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…