2013-01-15 50 views
3

我有一個標籤欄應用程序與導航控制器在應用程序的5個選項卡中的4個。我在我的應用程序中有一個「重置應用程序」功能,可以清除所有數據等......我也希望將所有視圖控制器彈回到其頂視圖。我知道如何使用popToRootViewControllerAnimated爲單個導航控制器彈出到根目錄,但可以彈出每個選項卡上的所有視圖控制器嗎?在標籤欄上彈出所有視圖控制器應用程序

回答

10

您需要通過viewControllers陣列tabBarController &流行到根視圖控制器枚舉如果控制器陣列的UINavigationController喜歡 -

for(UIViewController *viewController in tabBarController.viewControllers) 
{ 
    if([viewController isKindOfClass:[UINavigationController class]]) 
     [(UINavigationController *)viewController popToRootViewControllerAnimated:NO]; 
} 
+0

你打我吧。 –

+0

非常感謝! – Carl

+1

雖然我得到'不平衡的調用開始/結束外觀轉換' - 任何想法來解決它? – Teffi

0

你有另一種選擇是使用NSNotifications。我需要在關閉模式視圖(定時器上的幻燈片放映)時在我的所有選項卡上觸發popToRootViewController,並且這是我能夠弄清楚如何執行此操作的唯一方法。 我在模態視圖的viewWillDissapear方法中觸發了NSNotification,然後在我希望關閉的每個視圖中對其進行響應。

1

拉胡爾的答案是完美的解決方案。但是如果你的標籤欄有超過5個標籤,你會看到「更多」標籤。你需要顯式重置這個選項卡(只需將tabBarController.moreNavigationController彈出到rootViewController)。

這裏是代碼示例:

for(UIViewController *viewController in tabBarController.viewControllers) 
{ 
    if([viewController isKindOfClass:[UINavigationController class]]) 
     [(UINavigationController *)viewController popToRootViewControllerAnimated:NO]; 
} 
[tabBarController.moreNavigationController popToRootViewControllerAnimated:NO]; 

..

0
func popAll(){ 

    let tabBarController = window!.rootViewController as! UITabBarController 
    tabBarController.delegate = self 

    if let tabBarViewControllers = tabBarController.viewControllers { 


    let campusController = tabBarViewControllers[0] as! UINavigationController 

    let campusTVC = campusController.viewControllers[0] as! CampusTVC 

    _ = campusTVC.navigationController?.popToRootViewController(animated: false) 

    let adController = tabBarViewControllers[1] as! UINavigationController 

    let adminTVC = adminController.viewControllers[0] as! AdTVC 

    _ = adminTVC.navigationController?.popToRootViewController(animated: false) 

    let searchController = tabBarViewControllers[2] as! UINavigationController 

    let searchTVC = searchController.viewControllers[0] as! SearchTVC 
    _ = searchTVC.navigationController?.popToRootViewController(animated: false) 


    } 

} 

我的代碼在彈出斯威夫特所有選項卡實例。這是在我的應用程序委託。 這就是我在VC中的稱呼方式

let appDelegate = UIApplication.shared.delegate as! AppDelegate 
appDelegate.popAll() 
相關問題