Switch views with navigation controller using UISegmentedControl - swift

I have a UISegmentedControl which switches 2 View controllers. I want my view controllers will be root view controller for navigation controller. I create them like this:
filialView = (UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "FilialList") as! FilialListController).view
viewContainer.addSubview(filialView)
let navCon = (UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "MyScheduleNav") as! MyScheduleNavController)
myScheduleView = navCon.view
viewContainer.addSubview(myScheduleView)
self.viewContainer.bringSubviewToFront(self.filialView)
self.viewContainer.layoutIfNeeded()
But when I open another view controller from myScheduleView using segue there is no navigation bar on that view.
Could you help me please?

Related

Switch from one UIStoryBoard Controller to another UIStoryBoard Controller

Want to navigate between two UIStoryBoard and between different UIViewController. My problem is that when I tap on UIButton the control should transfer to the UIViewController from current UIStoryBoard to another UIStoryBoard.
Any help is appreciated.
You can either Push or Present the new viewController
Using Following code you can push the new view controller to your navigation controller
let yourVC = UIStoryboard(name: StoryBoardName, bundle: nil).instantiateViewController(withIdentifier: ViewConrtollerID) as! YourViewController
self.navigationController?.pushViewController(yourVC, animated: true)
You first need to create reference of the second storyboard in which you want to move from current. For that you need to create reference as shown in below image and set the other story board name in that.
after that you can push the to that storyboard controller using below code.
let vc = UIStoryboard(name:"storyboadnameToPush", bundle: nil).instantiateViewController(withIdentifier:controller storyboardId)
self.navigationController?.pushViewController(vc, animated: true)

IBOutlet nil - button

I have a HomeController and a registerController. The HomeController is embedded in a navigation controller and is the root view controller. If I present the registerController modally in HomeController's ViewWillAppear:
let reg = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "signup") as! RegisterController
reg.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
reg.modalTransitionStyle = UIModalTransitionStyle.flipHorizontal
reg.view.frame = self.view.frame
self.view.addSubview(reg.view)
There is no problem. BUT if I present it by pushing it on the navigation controller stack:
let v = RegisterController()
self.navigationController?.pushViewController(v, animated: true)
the app crashes and says the IBOutlet for the signUp button (which is in registerController) is nil. I've re-created the outlet and cleaned the project and restarted xcode and nothing has worked...
It’s not a “bug”. Your code is what’s at fault. It has nothing to do with pushing vs presenting. It’s that this line is wrong:
let v = RegisterController()
That creates a barebones view controller with no outlets hooked up. The outlets are hooked up in the storyboard instance of this class. Create the view controller from the storyboard as you did in the first code, and all will be well.
let v = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "signup") as! RegisterController

Open view controller programatically and dont see navigation bar. Swift 3

On my story board I have a collection view controller embedded in a navigation controller. Now from the cell of the collection view, I programaticly open the second view controller (called TwoPicsViewController), like this:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "TwoPicsViewController") as! TwoPicsViewController
self.present(nextViewController, animated:true, completion:nil)
However when my TwoPicsViewController opens, I don't see the navigation bar at all. On the story board I connected the TwoPicsViewController to the first view controller with a segue show.
Am I missing something else?
Thanks
To push a new UIViewController to the navigation stack you should use:
self.navigationController?.pushViewController(nextViewController, animated: true)
Instead of:
self.present(nextViewController, animated:true, completion:nil)
You are not putting the new vc on the navigation stack, try this:
performSegue(withIdentifier: "Segue Name", sender: nil)
And on Storyboard select the Segue, attributes inspector, identifier = "Segue Name"

Navigation bar dissapears when moving between two storyboards with Swift 3

The project I am working on currently has multiple storyboards. we are programatically moving from the first storyboard to the next using the following code:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "DiscoverViewController") as UIViewController
self.present(vc, animated: true, completion: nil)
the storyboard it is moving to has the Storyboard Entry Point pointed at the Navigation Controller of the view controller that is being instantiated and presented above.
The problem is that when the the page is displayed the navigation bar doesn't appear. It appears on the storyboard view, and before we separated the storyboards we weren't having this issue.
Is there an issue with the way I am going from one storyboard to the other? Do I need to present the navigation controller rather than the view controller?
thanks!
This is because you're presenting the view controller, when instead you probably want to push it onto the navigation stack.
This answer shows what you need to do. Here is the code, adapted for your example:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "DiscoverViewController") as UIViewController
self.navigationController?.pushViewController(vc, animated: true)

how to call existing navigationcontroller in Swift

I have 3 items in my storyboard. 1. viewController (A) connected to 2. Navigation controller and 3. viewController (B) is NOT connected to anything
All 3 items have restoration identifiers set in the storyboard.
viewController (B) is the initial view controller.
I am trying in B to get the navigation controller that A is attached to, but always returns nil:
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyBoard.instantiateViewControllerWithIdentifier("viewControllerA") as! ViewControllerA
print(vc.navigationController?) // always prints nil
why!?
UPDATE#1
I can't declare a UINavigationController like a view controller. I've tried setting navigationcontroller with the id of 'myNavigationController' as storyboardID:
When storyboard, I get this error
let navigationController = storyBoard.instantiateViewControllerWithIdentifier("myNavigationController") as! UINavigationController
print(self.navigationController!) // fatal error: unexpectedly found nil while unwrapping an Optional value
I've also tried setting the id in restoration identifier, It bombed earlier at the instantiating line
#ColdLogic, see what hits I get when I search the entire project for that identifier:
Because you never instantiated the navigation controller, you instantiated view controller A. You would want something like this if you want both the nav controller and the view controller to be setup like they are in the storyboard
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = storyBoard.instantiateViewControllerWithIdentifier("YourNavControllerIdentifier") as! UINavigationController
let vc = navigationController.topViewController as! ViewControllerA
Your code directly instantiates an object of type ViewControllerA. Which, unless you setup logic to do it, does not have a navigation controller by default.
This Worked for me!
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let navigationController = storyBoard.instantiateViewController(withIdentifier: "HomeNavigationContoller") as! UINavigationController
let viewController = storyBoard.instantiateViewController(withIdentifier: "HomeViewController") as! HomeViewController
navigationController.pushViewController(messageVC, animated: true)
self.present(navigationController, animated: true, completion: nil)
Use NavigationController identifier to access the NavigationController and ViewController A is already attached to it, so it will automatically get loaded to NavigationController