UINavigationController to UITabBarController - swift

The first part of my app is a Sign in section with built in Navigation controller. When the sign up section is completed and a new user is created I would like to proceed into a UITabBarController and will add seperate navigation controllers for each tab. Right now when sign up is complete and my UITabBarController is presented it still shows the UINavigationController from the first section of my app. How do I exit the UINavigationController once I enter the new UITabBarController?
And here is the code
import UIKit
class SignUpSecondViewController: UIViewController, BEMCheckBoxDelegate {
#IBAction func completeButtonAction(_ sender: Any) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "NewTabBarViewController") as! UIViewController
// Alternative way to present the new view controller
self.navigationController?.show(vc, sender: nil)
}

You should better use
self.navigationController?.present(vc, animated: true, completion: nil)
Generally there is no need for signup once main tabbar controller is up. In this case you can do this which will remove signup from memory.
let appDelegate = UIApplication.shared.delegate as! AppDelegate)
appDelegate.window?.rootViewController = vc

Related

How can i present programmatically made ViewController on button click

I have this custom UIViewController. It is not connected to anything storyBoard and want to present on clicking a button
class SeeListsViewController: UIViewController {
var tableView = UITableView()
override func viewDidLoad() {
super.viewDidLoad()
self.setUpTableView()
view.addSubview(tableView)
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
// Do any additional setup after loading the view.
}
}
How can i do that. I know the storyBoard way but this one has no identifier and it is all programmatically made
The solution below if you have SeeListsViewController in the storyboard and make sure the storyboard identifier is also SeeListsViewController:
let storyboard = UIStoryboard(name: "<Your storyboard name>", bundle: nil)
let listVC = storyboard.instantiateViewController(withIdentifier: SeeListsViewController.self)
self.present(listVC, animated: true)
A programmatic way to present if you don't have a view controller in the storyboard and all the UI components should be designed programmatically in your viewcontroller
let navVC = UINavigationController(rootViewController: SeeListsViewController())
navVC.modalPresentationStyle = .fullScreen
self.present(navVC, animated: true, completion: nil)

Navigate from a controller to another controller having a root controller between them

I have 3 controllers: RootController, FirstController and SecondController.
I want to navigate from RootController -> FirstController (here I press a button) -> Take me to SecondController.
How can I do this without Notifications and Observers and without to create a segue from that button to SecondVC because after I need to press < Back and again < Back.
This is what I've tried to do but is not working:
import UIKit
class FirstViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
#IBAction func goToSecondVC(_ sender: UIButton) {
// Go from first VC to second VC
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let showSecondVC = storyBoard.instantiateViewController(withIdentifier: "goSecondVC") as! SecondViewController
self.present(showSecondVC, animated:true, completion:nil)
}
}
For your code example to work your SecondViewController needs to have the Storyboard ID set to "goSecondVC". This is done in the Identity inspector in the third tab in the right pane. This will present your ViewController modally. However since you have a navigationController you most likely want to push the SecondViewController on the navigationStack like so:
navigationController?.pushViewController(secondVC, animated: true)

xcode swift4 how do I change the page in viewdidload

I cannot use segue on viewDidLoad.
I already check the segue is worded on another func.
When load the view check some condition then change view this is what I want.
override func viewDidLoad(){
super.viewDidLoad()
performSegue(withIdentifier: "go" , sender: self)
}
If you want to push your view controller on view did load then just use this code.
let storyBoard : UIStoryboard = UIStoryboard(name: "your_StoryBoard", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "VC_Identifier") as! TargerViewController
self.navigationController?.pushViewController(nextViewController, animated: true)
OR
You can set the target viewcontroller as root viewcontroller by check any condition.
Hope it will helps you out.
i prefer that don't use segue use as below
let storyboard = UIStoryboard(name: "YourStoryboard", bundle: nil)
let VC = storyboard.instantiateViewController(withIdentifier: "Your Identifier") as! Your ViewController
self.present(VC, animated:true, completion:nil)
user this for redirect from One VC To Another VC
Replace viewDidLoad to viewDidAppear solved my problem
override func viewDidAppear() {
super.viewDidLoad()
performSegue(withIdentifier: "go" , sender: self)
}

Present ViewController from UNUserNotificationCenterDelegate

In my UNUserNotificationCenterDelegate method I want to jump the user to a specific view controller. I can easily get the VC itself from the storyboard:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "beach")
What I'm not entirely sure of though is now how to actually present it, since I don't have an object handy to call presentViewController on.
If your root is navigationVC
if let nav = self.window?.rootViewController as? UINavigationController {
nav.pushViewController(vc, animated: true)
}

How to correctly display a tab controller in swift

I'm making a login screen for my app and everything works as intented until I try to present my main view after the login(which uses a Tab Bar Controller).
The only problem is that it displays just the first item on the tab bar. I have to press the other buttons for the to appear.
Im using this code:
//after login...
var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
var vc: TabBarViewController = storyboard.instantiateViewControllerWithIdentifier("MainController") as! TabBarViewController
self.presentViewController(vc, animated: true, completion: nil)
My guess is that I need to load them all at the same time, but I dont know...
This is how I did it recently. I loaded my tabBarController and the login screen together, once the user has logged in (or completed the first screen experience) you can modally dismiss the controller.
func showloginView() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let loginViewController: LoginTableViewController = storyboard.instantiateViewControllerWithIdentifier("LoginTVC") as! LoginTableViewController
self.window?.makeKeyAndVisible()
var viewController = storyboard.instantiateViewControllerWithIdentifier("LoginTVC") as! LoginTableViewController
let nav = UINavigationController(rootViewController: viewController)
self.window?.rootViewController?.presentViewController(nav, animated: true, completion: nil)
}
for displaying your tabBarController and editing of any of the tabBarItems
let tabBarController = self.window?.rootViewController as? UITabBarController
let tabBarRootViewControllers: Array = tabBarController!.viewControllers!
let nav = tabBarRootViewControllers[0] as? UINavigationController
Hope this helps =)
If you link an UIButton, you can open a UITabBarController using this (providing you're using a Storyboard)
#IBAction func openTabBar(sender: AnyObject) {
var tabBarController = self.storyboard?.instantiateViewControllerWithIdentifier("tabBarController") as! UITabBarController
self.presentViewController(tabBarController, animated: false, completion: nil)
}
That'll pop the current view and open the tab bar controller.