Navigation bar disappear after segue - ios

I'm trying to adding a navigation controller and to associate it with the home (and of course the controllers that are connected to the home)
this because i prefer to not have the navigation controller in the first 3 VC (storyboard entry, login and sign up). My problem is that after a simple self.performSegue(withIdentifier: "goToHome1", sender: self) from one of the first three VC the navigation bar disappear, is the first time i'm going to add a navigation controller and until now i always used only this formulaself.performSegue(withIdentifier: "goToXcontroller", sender: self) to switch from one controller to another so maybe i have to change something to fix this problem? I also tried to find some tutorial about NC but i didn't find something clear that help me with this.

You have to create segue to your UINavigationController instead of home view controller. You are skipping navigation controller by directly using segue to home view controller.
Rootviewcontroller of your navigation controller is Home view controller. So if you create a segue to your navigation controller, it will open your home view controller with navigation bar.

Related

How to unwind to the first view controller on a navigation stack

I have a problem with unwinding my view to login screen. The storyboard structure is as follow:
Storyboard Structure
The user flow for the app is as follow:
user login on LoginVC-> goes to main tab bar screen by modal segue-> on each tab bar item, I added right bar button on the navigation controller to access profile page, each tab bar item has independent navigation controller to keep the nav controller structure linear. -> once i click profile page button, profile page is presented modally -> when logout button on profile page is clicked, it triggers unwind segue and dismiss view controller
func logoutUser(){
//Networking.logoutUser()
print("It goes to login")
self.performSegue(withIdentifier: "unwindToLogin", sender: self)
}
The unwind segue was implemented on LoginVC on the leftmost VC.I connected unwind segue on the profile screen and call it "unwindToLogin"
I simply used performSegueWithIdentifier. However, the method does not get called and nothing happen to the view.
Edit 1:
Im wondering since i call profile page modally on tab bar vc, it couldnt find the unwindtologin.
If i simply use the instantiateviewcontoller to call login, will it clear my view controller stack ?
Edit 2:
Sorry, i forgot that when i check if a user is logged in, i use the code below:
if (FIRAuth.auth()?.currentUser != nil) {
self.storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
self.window?.rootViewController = self.storyboard?.instantiateViewController(withIdentifier: "TabBarViewController")
}
So actually they can't find the unwind method because the root view is not main login view controller.
Can anyone help ?
Move to any View controller using unwind segue.
For moves to any view controller when you clicking a button.
- (IBAction)unwindToCurrentController:(UIStoryboardSegue *)unwindSegue
{
}
Add these above lines to your loginViewController.m file (or) add
these lines to which view controller you want to move.
Actually you want to move to login view controller when clicking a
button. So,create a button or choose any button for this event.
Before doing this you want to add the above code to your login view
controller. Now add action to your button by dragging into Exit
option on view controller on the Top.
It shows an unwind option unwindToCurrentController.Click that option
and connect it.
Now Build and run your app. It works perfectly.
You need to create the unwind segue on every ViewController that you want to call it on. They can all use the same #IBAction destination in the login screen and they can all be called "unwindToLogin".
I see the unwind segue defined on the MainTabBarController. In your UserProfileController, you need to control-drag from the view Controller icon to the Exit icon and then select your #IBAction in the pop up. Then find that unwind segue in the UserProfileController in the Document Outline and make its identifier "unwindToLogin".
In response to your Edit 2:
Since your initial viewController was put into place programmatically, it isn't possible to unwind to the LoginViewController. In this case,
put the landing #IBAction in your MainTabBarController and have it set the self.window?.rootViewController to the login screen.
You could try this it Worked for me.
[self.presentingViewController dismissViewControllerAnimated:NO completion:nil];
I use it to dismiss a modal view so that I can return to the originating view.

Branching off from a tab bar controller?

I've got 3 view controllers. Two I have hooked up to a tab bar controller and one that I'm wanting to access when a user selects a cell on the second view controller in my tabbed views.
I'm wanting when the user hits "back" on the 3rd "detail" page for the user to be taken back to the 2nd view.
When I do this by just adding a button and segueing back to the 2nd VC, the tab bar is gone. I tried in my viewDidAppear to unhide the tab bar, but I guess going off of the tab bar controller messes up the navigation.
I tried creating a variable that was like "didHitBack" and on my "back" button on the 3rd view I'm creating a segue back to the Tab Bar Controller, and if "didHitBack" is true I do
_ self.tabBarController?.selectedIndex = 1
which takes me to the second page, but it's loading the first view in then going to the second page which looks bad.
I was thinking maybe there was a way to do "didHitBack" and setting the tab bar's initial view controller to the second one or something, but that all just seems very wrong.
Is there a "proper" way to do this?
To recap I have VC1 and VC2 that are hooked up to a Tab Bar Controller, I have a tableview on VC2 that on didSelectRow I'm going to VC3 which I do not want to be a part of the tabbed view controller, and when I hit back on VC3 I want to go back to VC2.
If you want to build a navigation stack, you should embed your view controller in a UINavigationController
So your tab bar would be hooked up to VC1 and NavVC. The root view controller of NavVC would be VC2.
You can then push a new view controller onto the stack using the navigation controller (or pop the view controller to go back) all within the confines of the tabBar.

Navigation bar is empty, created from storyboard

This is my story board:
Whenever I jump to a view controller embedded in navigation controller, the navigation bar is shown but empty, why?
The sequence I created it is:
connect buttons with destination view controllers
embed destination view controllers in navigation view controller
And the segue I use is present modally - cross dissolve.
The First root controllers of a navigation controller won't have any Back button attached to its navigation bar. You should add an additional View Controller next to any root View Controller of Navigation Controller with Push Segue ( or Show Segue for newer IOS ) to navigate between them.
I tested different segue transition methods with test projects, the answer I got is: if you are transitioning by presenting it modally, you don't get the back button, you only get it by push.

Adding a Back Button on A Table View Controller

I am interested in implementing a back button to segue to a previous view controller. I have tried to embed a navigation bar onto the the top of the table view controller but the issue is when I do segue back to the first view controller, by default, Xcode keeps the navigation bar with a back button to go back to the table view controller. Is there a simpler way to implement a segue to go back to the first VC without the navigation bar remaining?
I'm not too sure if this works, but embed your view controllers including the first one inside the navigation controller. That would make all your view controllers with navigation bar above.
On the main view controller (the one you do not want to have the navigation bar), add the line of code inside your viewDidLoad method.
Swift 3:
self.navigationController?.navigationBar.isHidden = true
I found an easy way. On your TableViewController, drag a UIview to the top of the view controller and itll let you insert the view. From there just add your back button
Just assign your "Back" UIBarButtonItem to this method and it should work.
#IBAction func backButtonPressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: nil)
}
Sounds like a few problems
1) How are you navigating from the first view controller? Is it with Show (Push) if not, you are replacing the first view controller in your stack.
2) Based on your problem, make sure your first view controller is not embedded in a navigation controller
3) As Dylan mentioned, you need to hook your back button to remove the current view controller to return to the first one
4) Extension of (3), how are you segueing back to the first view controller? If you are explicitly navigating to first view controller with your back button handler, it's not actually going back but rather forward and keep the navigation bar.

Push segue from a view controller controlled by UITabBarController

Let's assume that a first view controller is connected with a UITabBarController and I want to make a push segue to the second view controller from this first view controller.
From my googling, it seems that a modal segue from a view controller connected with a UITabBarController hides the bottom tab bar, while a push segue doesn't.
However, my push segue is also hiding my tab bar in the second view controller. I have overridden prepareForSegue method in the first view controller.
Below are images of my storybard and the simulator. Anyone has an idea why this is the case? Thank you in advance for your helps.
Your trouble is because your tabViewController is embedded in the navigation stack that you initialise with your login screen.
you need to rearrange things so that each of your tab bar controller tabs opens to a new navigation stack.
What I suggest
your loginscreen should navigate to your tab bar controller with a modal/presenting segue, not a push segue. Remove the navController that encloses the loginscreen, you don't need it (well, even if you keep it, don't use a push segue, use a modal segue, and you won't then be referring back to that navController's viewController stack from inside your tab bar).
embed each of the first viewControllers in your tabViewCOntroller inside a separate navController.
Now you can push segue within your tabViewController's tabs.

Resources