xcode 退出导航控制器并返回标签栏视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40994943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 09:38:01  来源:igfitidea点击:

Get out of navigation controller and go back to Tab Bar View

swiftxcodeuistoryboarduistoryboardsegueunwind-segue

提问by Chris Mikkelsen

So I have this Storyboard.

所以我有这个故事板。

enter image description here

在此处输入图片说明

And this is the code when you click the save button.

这是单击保存按钮时的代码。

@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")
        }
    }

}

I'd like to get out of Create Group View Controller and go back to Tab Bar Controller. Perform segue works, but it will show a "Back" navigation item bar button.

我想退出创建组视图控制器并返回标签栏控制器。执行 segue 工作,但它会显示一个“返回”导航项栏按钮。

回答by Juan Curti

You can dismiss the navigation view. Check that your view is:

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

-TabBar
 -Navigation
  -View Controller
   -ViewX

Option 1: You can call from ViewX:

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

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

or

或者

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

Option 2: Use a protocol

选项 2:使用协议

To use a protocol, you should set the delegate variable in your ViewX and call it when you want to dismiss the whole navigation controller.

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

In the top of ViewX set:

在 ViewX 集的顶部:

protocol ProtocolHideNavigation{
    func hideNavigation();
}

Declare this in your ViewX:

在您的 ViewX 中声明:

var delegateHideNav:ProtocolHideNavigation?

When you want to hide your Navigation controller, call the delegate method with:

当你想隐藏你的导航控制器时,调用委托方法:

delegateHideNav.hideNavigation()

Go to your View Controller (The parent of ViewX). When you instance ViewX, add this:

转到您的视图控制器(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

In your View Controller declaration, add the protocol

在您的视图控制器声明中,添加协议

class ViewController : UIViewController, ProtocolHideNavigation{...

and add the method:

并添加方法:

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

回答by Durdu

As mentioned above it is not really clear/visible but have you tried ?

如上所述,它不是很清楚/可见,但您尝试过吗?

self.navigationController?.popToRootViewController(animated: true)

回答by Durdu

if you want to select a new tabitem

如果你想选择一个新的tabitem

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

make sure the window is the one you need - some framework items create their own views ( players/modals )

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

回答by Mehul

So what you want to do is simply switch the tab in the tab bar.?If you want to get out of your current controller and then switch the tab try:

所以你想要做的只是切换标签栏中的标签。?如果你想退出当前的控制器,然后切换标签尝试:

//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
}

回答by Chris Mikkelsen

I changed this code

我改变了这个代码

//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 })

Into this

成这个

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()

And now it goes back to the beginning of the storyboard.

现在它回到故事板的开头。

回答by Bogdan Bystritskiy

If you did from main screen showSegue to vc, after showDetail vc, you can back to main screen and choose tab this code:

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

Swift 4:

斯威夫特 4:

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

回答by Federico Malagoni

If you want to go to the root viewcontroller and you have a NavigationController embedded that is the code you have to use:

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

navigationController?.popToRootViewControllerAnimated(true)