Whats the programmatic opposite of showViewController:sender:

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)