Embed ViewController in NavigationController when it is already presented - swift - programmatically - swift

Is it possible to embed a ViewController in a UINavigationController when it is already presented?
I know that to present a ViewController in a NavigationController you should initialise it before like this:
let navigationController = UINavigationController(rootViewController: viewController)
self.present(navigationController, animated: true)
But I would like to know if it is possible to present a VC:
self.present(viewController, animated: true)
and once it is presented to embed it inside a NavigationController.

Related

UINavigationController to UITabBarController

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

Dismissing the tabbar view controller in swift

I have loginViewController and I am pushing a tabBarViewcontroller which is has 3 tabs
Below's my code:
self.dismiss(animated: false, completion: nil)
var profileStoryBoard: UIStoryboard!
profileStoryBoard = UIStoryboard(name:"Login", bundle: nil)
let viewcontroller: MainViewController = profileStoryBoard.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
self.navigationController?.pushViewController(viewcontroller, animated: false)
I then dismiss the loginViewController and push the tabBarViewController.
Now I have a sign out button on all 3 tabBar items, but it is not working.
Here's my code:
var profileStoryBoard: UIStoryboard!
profileStoryBoard = UIStoryboard(name:"Main", bundle: nil)
let viewcontroller : LoginViewController = profileStoryBoard.instantiateViewController(withIdentifier: "LoginViewController") as! LoginViewController
self.navigationController?.pushViewController(viewcontroller, animated: false)
I am new to swift so I don't know what mistake I might be doing here.
What you are doing is pushing another controller on top of the stack
self.navigationController?.pushViewController(viewcontroller, animated: false)
You instead want to go back one label. You should use this:
navigationController?.popViewController(animated: true)

show segue from modal viewController

I am trying to implement a push/show segue from a UITableViewController that is presented modally. I am able to perform a push segue from a row in the tableview, but it is delayed. I know it has something to do with the navigation hierarchy stack, and I have tried many things like resetting the rootViewController but am not having any luck...thanks for any help!
// present place tableViewController ---> modally
func handleShowPlacesVC() {
let vc = PlacesTableVC()
let navigationController = UINavigationController(rootViewController: vc)
present(navigationController, animated: true, completion: nil)
}
// show details tableViewController ---> push segue from tableview
func handleShowCurrentUserPlaceDetailsVC() {
let vc = CurrentUserPlaceDetailsVC()
navigationController?.pushViewController(vc, animated: true)
}

Swift, present a new uiview with tab bar

I am trying to present a new view after click button in a tableview cell. The view can show up but without the tab bar. Is there any solution that showing the view with tab bar? Thanks.
Storyboard Screenshot
Using segue or progammatically are not right.
func viewDetailAction(sender: UIButton){
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("patientDetailVC ") as! PatientDetailViewController
// self.presentViewController(vc, animated: true, completion: nil)
// self.navigationController?.pushViewController(vc, animated: true)
self.performSegueWithIdentifier("patientDetailCell", sender: self)
}
A easier way is to present the VC yourself
self.presentViewController(vc, animated:true, completion:nil)
This will present it on your current VC and not on the tab barController as it is being done probably in the storyboard segue
Please ignore any code syntax mistakes.
Try,
vc.modalPresentationStyle = .OverCurrentContext
after instantiateViewControllerWithIdentifier.
Edit: (using segue)
Simply, delete show segue and use Present Modally segue, click on segue, be sure Over Current Context presentation.
And then use in your action only:
self.performSegueWithIdentifier("overNewPage", sender: nil)
You can reach its properties using prepareForSegue method.
Edit: (using instantiateViewControllerWithIdentifier)
let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("patientDetailVC") as! PatientDetailViewController
viewController.modalPresentationStyle = .OverCurrentContext
viewController.someString = "youCanPassData"
self.presentViewController(viewController, animated:true, completion:nil)

Swift: How do I get access to the navigationController in AppDelegate

I have added a NavigationController in the StoryBoard and made it the initial viewController.
How do I get access to the NavigationController in the appDelegate. Ctrl-drag from navigationController to AppDelegate does not create an outlet.
In didFinishLaunchingWithOptions:
let navigationController = application.windows[0].rootViewController as! UINavigationController
In custom functions use like this:
let navigationController = self.window?.rootViewController as! UINavigationController