是什么与showViewController的编程相反:sender: [英] Whats the programmatic opposite of showViewController:sender:

查看:398
本文介绍了是什么与showViewController的编程相反:sender:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个仅限iOS 8的应用程序,我正在使用Show和Show Detailsegue以及 showViewController:sender: showDetailViewController: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.

我的问题是在调用 showViewController:sender:之后返回的编程方式是什么?视图控制器的显示方式取决于其父上下文。例如。在 UINavigationController showViewController:sender:将新控制器推送到导航堆栈但是如果没有UIKit容器查看控制器图表然后 showViewController:sender:最后做一个演示文稿。

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];
}
...

等...所以有一个通用的反转显示的方式?如果不是我看到的唯一解决方案是使用展开segue的一切。没有太多的麻烦,但我很好奇。

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.

推荐答案

有一章关于如何 showViewController :sender: showDetailViewController:sender:编程iOS 8:深入视图,视图控制器和框架

调用这些方法时,它们调用 targetViewControllerForAction:sender :自己并在返回的对象上调用此方法。然后,目标对象可以以适当的方式显示视图控制器。例如,导航控制器在其导航堆栈上推送视图控制器。

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.

因此,您可以创建泛型 dismissVC:方法并在不同的 UIViewController 子类中覆盖它。

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()
    }
}

这样,当你打电话给 dismissVC:方法,如果总是根据上下文正确地关闭视图控制器。

This way, when you call dismissVC: method, if will always correctly dismiss the view controller depending on the context.

这篇关于是什么与showViewController的编程相反:sender:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆