简体   繁体   English

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

[英]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.

相关问题 Swift 3:如何转到上一个ViewController并重新加载它? - Swift 3 : How can I go to the previous ViewController and reload it? 导航到storyboard中的viewcontroller始终会创建一个新的ViewController,如何重用以前的ViewController? - Navigating to a viewcontroller in storyboard always creates a new ViewController, how can I reuse the previous ViewController? 如何在 Swift 3 中检查当前的 ViewController? - How to check Current ViewController in Swift 3? 在使用Swift展示新的Viewcontroller后如何关闭以前的Viewcontroller? - How to dismiss previous Viewcontroller after presenting new Viewcontroller using Swift? 如何在iOS swift中将数据传递给以前的viewController? - how to pass data to previous viewController in iOS swift? 如何检查以前的ViewController是否打开 - how to check what previous viewcontroller was open 如何在Swift 2中的特定方向上正确锁定ViewController? - How do I properly lock my viewController in a specific orientation in swift 2? Swift-如何使用从ViewController到特定TabBarController的按钮进行搜索 - Swift - How do I segue with a button from ViewController to a specific TabBarController 如何检查我的用户是否已经登录(会话)并转到特定的ViewController? - How can I check if my user is already logged in (session), and go to specific ViewController? 斯威夫特:如何传递子视图控制器prepareForSegue的数据以更改父视图控制器表视图单元格的TextLabel? - Swift: How can I pass data from child viewcontroller prepareForSegue to change the TextLabel of a cell of parent viewcontroller tableview?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM