swift - 如何检查特定的viewController是否是之前的viewController

时间:2017-07-05 12:02:46

标签: ios swift3 swrevealviewcontroller

我正在尝试检测我之前的viewController是否是一个特定的,如果是我按回来它将加载tabBar控制器。 我正在使用revealViewController更改视图控制器 这是我的代码: 在这里我保存了以前的视图控制器:

let newVC = 
self.storyboard?.instantiateViewController(withIdentifier: 
storyboardIdentifiers.newViewControllerID) as! newViewController         
newVC.previousVC = self
self.revealViewController().setFront(newVC, animated: true)

这是我的后退动作,我需要检查以前是否是第一个viewController

func backAction() {
let first  = self.storyboard?.instantiateViewController(withIdentifier: storyboardIdentifiers.firstViewControllerID) as! firstViewController
let second = self.storyboard?.instantiateViewController(withIdentifier: storyboardIdentifiers.secondViewControllerID) as! secondViewController

if previousVC  ==  first || previousVC == second {
            previousVC = self.storyboard?.instantiateViewController(withIdentifier: storyboardIdentifiers.revealViewControllerID) as! SWRevealViewController
self.revealViewController().setFront(previousVC, animated: true) 
}
else {
 self.revealViewController().setFront(previousVC, animated: true)
}

但是当我按下它时,它没有检测到它来自其中一个视图控制器。

当我打印“self”时,它会给我这个结果

<MyPackege.firstViewController: 0x7f9e80f2b5a0>

但它没有加载TabBarController

1 个答案:

答案 0 :(得分:1)

而不是使用==运算符尝试使用isKind(of: )方法检查viewcontroller的类型

func backAction() {
let first  = self.storyboard?.instantiateViewController(withIdentifier: storyboardIdentifiers.firstViewControllerID) as! firstViewController
let second = self.storyboard?.instantiateViewController(withIdentifier: storyboardIdentifiers.secondViewControllerID) as! secondViewController

if previousVC.isKind(of:firstViewController ) || previousVC.isKind(of:secondViewController ) {
            previousVC = self.storyboard?.instantiateViewController(withIdentifier: storyboardIdentifiers.revealViewControllerID) as! SWRevealViewController
self.revealViewController().setFront(previousVC, animated: true) 
}
else {
 self.revealViewController().setFront(previousVC, animated: true)
}