1

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.

Chris Mikkelsen
  • 3,987
  • 9
  • 29
  • 41

7 Answers7

3

if you want to select a new 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 )

Durdu
  • 4,649
  • 2
  • 27
  • 47
2

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

self.navigationController?.popToRootViewController(animated: true)
Durdu
  • 4,649
  • 2
  • 27
  • 47
2

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

-TabBar
 -Navigation
  -View Controller
   -ViewX

Option 1: You can call from ViewX:

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

or

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

Option 2: Use a protocol

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.

In the top of ViewX set:

protocol ProtocolHideNavigation{
    func hideNavigation();
}

Declare this in your 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:

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)
}
J Manuel
  • 3,010
  • 22
  • 39
0

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

navigationController?.popToRootViewControllerAnimated(true)
Federico Malagoni
  • 722
  • 1
  • 7
  • 21
0

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
}
Mehul
  • 572
  • 4
  • 17
0

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.

Chris Mikkelsen
  • 3,987
  • 9
  • 29
  • 41
0

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

Swift 4:

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