2017-07-05 24 views
0

我想檢測如果我以前的viewController是一個特定的,如果它是當我按回它將加載tabBar控制器。 我改變與revealViewController 視圖控制器這裏是我的代碼: 在這裏,我保存以前的視圖控制器:swift - 如何檢查如果一個特定的viewController是以前的viewController

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

但是當我按回它並沒有檢測到它來自這些視圖控制器之一。

當我打印的「自我」它給了我這樣的結果

<MyPackege.firstViewController: 0x7f9e80f2b5a0> 

但它不會加載TabBarController

+0

請問您能詳細說明一下嗎? 「如果我以前的viewController是一個特定的」,那麼你想要做什麼?加載相同的VC或者加載另一個? –

+0

我想加載前一個,但用TabBarController加載它 –

回答

1

而不是使用==運營商嘗試使用isKind(of:)方法來檢查的視圖 - 控制

類型
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) 
} 
+0

謝謝!它工作完美! –

+0

如果你可以接受這個答案,這樣可以幫助別人 –

相關問題