Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whats the programmatic opposite of showViewController:sender:

I'm writing an iOS 8 only app and I'm using the new adaptive presentations using a combination of the "Show" and the "Show Detail" segue and the showViewController:sender: and showDetailViewController:sender: methods.

My question is what is the programatic way to go back after calling showViewController:sender:? The way the view controller is shown depends on its parent context. E.g. in a UINavigationController showViewController:sender: pushes a new controller onto the navigation stack but if there is no UIKit container in the view controller graph then showViewController:sender: ends up doing a presentation instead.

Considering i could write my own arbitrary container controller it seems unfeasible to check

if (self.navigationController) {     [self.navigationController popViewControllerAnimated:YES]; } else if (self.presentingViewController){ ... else if ([self.parentViewController isKindOfClass:[CrazyCustomContainer class]]){     [self.parentViewController someWackyUnwindMethod]; } ... 

etc... so is there a generic way to reverse being shown? If not the only solution i see is to use unwind segues for everything. Not too much of a hassle but I'm curious.

like image 861
jackslash Avatar asked Sep 09 '14 11:09

jackslash


People also ask

What is a UIViewController?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.

How do I push a presented ViewController?

VC3 -> present VC2 -> VC1 VC2 needs to be in a UINavigationController then after you present it you can push VC3. The back button will work as expected on VC3, for VC2 you should call dismiss when the back button is pressed. Try implementing some of that in code and then update your question.


1 Answers

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.

like image 140
bzz Avatar answered Oct 05 '22 15:10

bzz