2016-12-06 112 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() 

而现在它可以追溯到故事板的开始。

相关问题