2016-12-06 166 views
0

所以我有這個故事板。離開導航控制器,並返回到標籤欄視圖

enter image description here

這是當您單擊保存按鈕的代碼。

@IBAction func addGroupAction(_ sender: Any) { 
    self.name = groupNameTextField.text! 

    //show spinner progress dialog 
    self.hud.textLabel.text = "Loading..." 
    self.hud.show(in: self.view) 

    DispatchQueue.global(qos: .background).async { 
     if !self.uuid.isEmpty { 
      self.service.groupCreate(uuid: self.uuid, name: self.name, type: "regular", callback: { 
       status, message, json in 
       DispatchQueue.main.async { 
        print(status) 
        print(message) 
        print(json) 

       } 

       self.hud.dismiss() 

       //this block of code does not work 
       let storyboard = UIStoryboard(name: "Main", bundle: nil) 
       let controller = storyboard.instantiateViewController(withIdentifier: "MainTabbar") as! UITabBarController 
       self.present(controller, animated: true, completion: {() -> Void in }) 

      }) 
     } else { 
      print("no uuid") 
     } 
    } 

} 

我想擺脫創建組視圖控制器並返回到選項卡欄控制器。執行segue作品,但會顯示一個「Back」導航項目欄按鈕。

+0

您的圖像不可見。 Tab Bar Controller是默認的UITabBarController還是一些自定義控制器? – Mehul

+0

是的這是一個默認的TabBarController –

+1

得到你想要去任何控制器的實例。然後將其設置爲根視圖控制器..多數民衆贊成它! – CaffeineShots

回答

0

如上所述,它不是很清楚/可見,但你試過了嗎?

self.navigationController?.popToRootViewController(animated: true) 
+0

它只讓我回到以前的ViewController。我想回到選項卡欄的第一個視圖控制器。 –

0

如果你想要去根視圖控制器,你有一個NavigationController嵌入式那就是你必須使用代碼:

navigationController?.popToRootViewControllerAnimated(true) 
+0

它只把我帶回到以前的ViewController。我想回到選項卡欄的第一個視圖控制器。 –

+0

'self.view.window!.rootViewController?.dismissViewControllerAnimated(true,completion:nil)'這樣的事情? –

+1

類似於?:'self.view.window!.rootViewController?.dismissViewController Animated(true,completion:nil)' –

0

您可以關閉該導航視圖。 檢查你的看法是:

-TabBar 
-Navigation 
    -View Controller 
    -ViewX 

選項1:您可以從ViewX撥打:

self.parent?.parent?.dismiss(animated: true, completion: nil) 

let _ = self.parent?.navigationController?.popViewController(animated: true) 

選項2:使用協議

T o使用協議,您應該在您的ViewX中設置委託變量,並在您想關閉整個導航控制器時調用它。

在ViewX的頂部設置:

protocol ProtocolHideNavigation{ 
    func hideNavigation(); 
} 

聲明這個在您的ViewX:

var delegateHideNav:ProtocolHideNavigation? 

當你想隱藏自己的導航控制器,調用委託方法有:

delegateHideNav.hideNavigation() 

轉到您的視圖控制器(ViewX的父級)。當你實例ViewX,補充一點:

let viewXC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "viewXVC") as! ViewX //You already have this 
viewXC.delegateHideNav = self 
self.navigationController?.pushViewController(viewXC, animated: true) //This too 

在您的視圖控制器聲明,添加的協議

class ViewController : UIViewController, ProtocolHideNavigation{... 

,並添加方法:如果你想選擇一個新的

func hideNavigation(){ 
let _ = self.navigationController?.popViewController(animated: true) 
} 
0

tabitem

if let window = UIApplication.shared.delegate?.window { 
     if let myTabController = window?.rootViewController as? UITabBarController{ 
      myTabController.selectedIndex = 1 
      myTabController.selectedViewController = myTabController.viewControllers?[1] 
     } 
    } 

確保窗口是你需要的 - 一些框架項目創建自己的意見(球員/模式)

0

所以你想要做的只是切換標籤欄中的選項卡。如果你想獲得你的電流控制器,然後切換選項卡嘗試:

//To go to the root view controller 
self.navigationController?.popToRootViewController(animated: true) 

//To access the tab bar instance 
if let tabBarController = self.window!.rootViewController as? UITabBarController { 

    //Change the selected index to the one you want (starts from 0) 
    tabBarController.selectedIndex = 1 
} 
0

我改變了這種代碼

//this block of code does not work 
let storyboard = UIStoryboard(name: "Main", bundle: nil) 
let controller = storyboard.instantiateViewController(withIdentifier: "MainTabbar") as! UITabBarController 
self.present(controller, animated: true, completion: {() -> Void in }) 

進入這個

let tabBar = self.instantiateViewController(storyBoard: "Main", viewControllerID: "MainTabbar") 
let appDelegate = UIApplication.shared.delegate as! AppDelegate 
appDelegate.window = UIWindow(frame: UIScreen.main.bounds) 
appDelegate.window?.rootViewController = tabBar 
appDelegate.window?.makeKeyAndVisible() 

而現在它可以追溯到故事板的開始。

相關問題