退出导航控制器并返回Tab Bar View

时间:2016-12-06 12:02:54

标签: swift xcode uistoryboard uistoryboardsegue unwind-segue

所以我有这个故事板。

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工作,但会显示“返回”导航项目栏按钮。

7 个答案:

答案 0 :(得分:2)

如上所述,它不是很清晰/可见,但你试过吗?

self.navigationController?.popToRootViewController(animated: true)

答案 1 :(得分:0)

如果您想转到根视图控制器并且嵌入了一个您必须使用的代码的NavigationController:

navigationController?.popToRootViewControllerAnimated(true)

答案 2 :(得分:0)

您可以关闭导航视图。 检查您的观点是否为:

-TabBar
 -Navigation
  -View Controller
   -ViewX

选项1:您可以从ViewX调用:

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

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

选项2:使用协议

要使用协议,您应该在ViewX中设置委托变量,并在想要关闭整个导航控制器时调用它。

在ViewX集的顶部:

protocol ProtocolHideNavigation{
    func hideNavigation();
}

在ViewX中声明:

var delegateHideNav:ProtocolHideNavigation?

如果要隐藏导航控制器,请使用以下命令调用委托方法:

delegateHideNav.hideNavigation()

转到View Controller(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

在View Controller声明中,添加协议

class ViewController : UIViewController, ProtocolHideNavigation{...

并添加方法:

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

答案 3 :(得分:0)

如果你想选择一个新的tabitem

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

确保窗口是您需要的窗口 - 一些框架项创建自己的视图(播放器/模态)

答案 4 :(得分: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
}

答案 5 :(得分: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()

现在又回到了故事板的开头。

答案 6 :(得分:0)

如果您从主屏幕showSegue到vc,在showDetail vc之后,您可以返回主屏幕并选择标签此代码:

Swift 4:

self.dismiss(animated: false, completion: {
  self.navigationController?.popToRootViewController(animated: true)
  if let tabBarController = appDelegate.window!.rootViewController as? TabBarVC {
    tabBarController.selectedIndex = 4
  }
})