[英]swift - how can i check if a specific viewController is the previous viewController
I am trying to detect if my previous viewController is one specific and if it is when i press back it will load the tabBar controller. 我正在尝试检测我以前的viewController是否是特定的,如果是当我按回时它将加载tabBar控制器。 I am changing view controllers with revealViewController here is my code : here i save the previous view controller:
我正在使用revealViewController更改视图控制器,这是我的代码:在这里,我保存了以前的视图控制器:
let newVC =
self.storyboard?.instantiateViewController(withIdentifier:
storyboardIdentifiers.newViewControllerID) as! newViewController
newVC.previousVC = self
self.revealViewController().setFront(newVC, animated: true)
this is my back action where i need to check if previous is the first viewController 这是我的后动作,我需要检查以前是否是第一个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)
}
but when i press back it doesn't detect that it is from one of these view controllers. 但是当我按回它时,它不会检测到它来自这些视图控制器之一。
when i print "self" it gives me this result 当我打印“自我”时,它给我这个结果
<MyPackege.firstViewController: 0x7f9e80f2b5a0>
but it doesn't load the TabBarController 但它不会加载TabBarController
Instead of using ==
operator try using isKind(of: )
method to check the type of viewcontroller 而不是使用
==
运算符,请尝试使用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)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.