Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

ios - 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.

question from:https://stackoverflow.com/questions/25742944/whats-the-programmatic-opposite-of-showviewcontrollersender

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...