2016-03-30 54 views
1

在我的swift應用程序中,我有一個帶有按鈕的UIViewController。這個按鈕打開UIViewController 2號,用戶有另一個按鈕。當用戶按下它 - 他打開的UIViewController號3也有一個按鈕,當用戶按下它 - 他稱之爲代碼:有沒有在Swift中同時解僱兩個uiViewController的方法?

self.dismissViewControllerAnimated(false, completion: nil) 

並且由於它的UIViewController 3號消失,用戶看到的UIViewController 2號。我的問題是 - 是否還有一種方法可以解除UIViewController編號2,以便用戶可以平穩地從編號3回到編號1?

現在我創建了一個函數,並調用它通過協議:

UIViewController中編號2:

protocol HandleYourFullRequest: class { 
    func hideContainer() 
} 


class FullRequest: UIViewController, HandleYourFullRequest{ 

    func hideContainer(){ 
     self.dismissViewControllerAnimated(false, completion: nil) 
    } 

    @IBAction func exitbuttonaction(sender: AnyObject) { 
     self.performSegueWithIdentifier("temporarySegue", sender: self) 
    } 

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
    if (segue.identifier == "temporarySegue"){ 


     if let fullRequestDetails = segue.destinationViewController as? YourFullRequest 
     { 

      fullRequestDetails.delegateRequest = self 
     } 

    } 
    } 



} 

的UIViewController號3:

class YourFullRequest: UIViewController{ 

    var delegateRequest:HandleYourFullRequest? 

    @IBAction func exitbuttonaction(sender: AnyObject) { 
     self.dismissViewControllerAnimated(true, completion: nil) 

     delegateRequest?.hideContainer() 
    } 
} 

但是與該解決方案時,用戶按下按鈕 - UIViewController編號3消失,UIViewController編號2出現一秒鐘然後消失。有沒有一種方法可以在不顯示給用戶的情況下刪除2號並將他直接指向號碼1?

回答

2

我還不清楚其中兩個按鈕連接到哪個動作,而是從按下視圖控制器上有什麼我可以告訴當解僱按鈕3它視圖控制器調用數self.dismissViewControllerAnimated(false, completion: nil) 2.

嘗試把這個方法在視圖控制器3

@IBAction func exitButtonAction(sender: AnyObject) { 
    self.presentingViewController?.presentingViewController?.dismissViewControllerAnimated(true, completion: nil); 
} 

這將工作假設兩個視圖控制器都在像導航控制器未按。

0

您可以使用popToViewController(viewController: UIViewController, animated: Bool)

哪裏viewController是要流行到,你的情況, 'UIViewController中數字1' 的viewController。

popToViewController Documentation

如果你沒有到視圖控制器的引用,你可以從self.navigationController.viewControllers得到它,它會在你的榜樣的第一個對象。

+0

你能給我一個提示如何引用'self.navigationController.viewControllers'的第一個元素?我嘗試了'popToViewController(self.navigationController.viewControllers.first:UIViewController,animated:Bool)',但是在我的情況下會導致一些錯誤:( – user3766930

+0

@ user3766930在彈出視圖控制器並打印出自己的內容之前設置一個斷點。 navigationController.viewControllers array。在這裏看看有用,你的視圖控制器層次結構可能不是你所描述的。 – Tim

0

UINavigationController上有一個名爲setViewControllers的方法。它需要一系列所有您想要活動的視圖控制器。您可以將堆棧中的所有視圖控制器作爲數組取出,刪除不想要的視圖控制器,然後使用更新後的數組調用setViewControllers。

+0

然而,感謝你的線索 - 你能告訴我一些例子,可能與我目前的代碼 - 我可以如何使用它?我真的很感激! – user3766930

+0

如果讓myNav = self.navigationController { 變種allvc = myNav.viewControllers allvc.removeLast() allvc.removeLast() myNav.setViewControllers(allvc,動畫:真) } –

0

您可以使用removeLast()函數將控制器從堆棧中彈出。

@IBAction func doneAction(sender: AnyObject) { 
    var vc = self.navigationController?.viewControllers 
    // remove controllers from the stack 
    vc?.removeLast() 
    vc?.removeLast() 
    // Jump back to the controller you want. 
    self.navigationController?.setViewControllers(vc!, animated: false)   
} 
+0

HMM的我嘗試這樣做的解決方案,但是當我按下按鈕 - 沒有任何工作:)我不確定是否有什麼我可能會錯過? – user3766930

+0

「無效」意味着什麼都沒有發生?你的按鈕是否掛鉤了該功能? – ryantxr

+0

是的,我只看到了一個打印味精,我在var vc = self.navigationController上面添加了......' – user3766930