Changing views programmatically - swift

I know there are a lot of posts about this but my problem is a bit different. I have a Tab Bar Controller with multiple views and one of the views is the Login View, if the user does Login it should change the view to present the User menu.
At the same time i need to keep the Tab Bar menu visible, so i can't use modal since it will loose the reference to the Tab Bar Controller.
So i ended up using a Navigation Controller where i just push the User Menu view into the stack (not the best way but it works).
The problem is, if the user double click on the Login Bar it will load the Login View, and i don't want this to happen.
On this example, if the user clicks on the Login button it will go to the Blue view, but if the user double click on the Feature Button on the Tab Bar Menu it will go to the red view.
Since my solution does not work how can i achieve what i want? If the user press the Login button it will ONLY show the Blue view until he press the Logout button.
[Edit]
Here is the code to show the blue view:
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let ViewController = storyBoard.instantiateViewController(withIdentifier: "userViewID") as! UserViewController
self.navigationController?.pushViewController(ViewController, animated: true)

Ok i ended up checking if the Navigation Controller had more than one child (for the red one) and removing the first child from the navigation array so it could dealloc it and show only the one i wanted.
override func viewWillAppear(_ animated: Bool) {
if((self.navigationController?.viewControllers.count)! > 1)
{
self.navigationController?.viewControllers.removeFirst()
}
}
The reason i ended up doing this was because clicking 2x on the Tab Bar item it would bring me to the red view even if i had already pushed the blue view to the Navigation array.

Related

How can I make a Back button on a Tabbed View App in Xcode

I have already multiple views that are in the tab bar at the bottom. In one of those view controllers (home view), I made a button connecting to another page (feed view) that isn't in the tab bar.
How can I make a back button from the feed view to the home view?
I've searched and I've only found tutorials for single view apps.
Reminder: the feed view isn't in the tab bar.
Add a Navbar then connect the left button(Back Button) to the view controller and do self.dismiss(animated: true, completion: nil) inside of that button action event
or
Just link a button from the view and do
self.dismiss(animated: true, completion: nil)
The way to go from tab view to a separate view use this code inside of the button action to go to a non TabBar view
let objSecondVc = self.storyboard?.instantiateViewController(withIdentifier: "ViewController") as? ViewController
objSecondVc!.modalPresentationStyle = .fullScreen
present(objSecondVc!, animated: true, completion: nil)
Then in the seperate View Controller to go back to the TabBar View use
self.dismiss(animated: true, completion: nil)
There is a multiple ways you can do this:
1)
To go to the feed view use present() and then if you want to get back use dismiss()
and the put dismiss function inside of an action of a back button you create in the feed view that you may put it the left upper corner of the view for instance.
2)
You can use NavigationController to go to the feed view and back using the navigation back button.
for me I use the first one because it is more flexible and because I use code instead of storyboard it is easier for me.(Thats just my opinion)

Segue as a new view

I have a navigation controller as a root view to the home view then a sign in view then the main view. When I sign in and segue to the main view using
vc.performSegueWithIdentifier("toMenuView", sender: nil)
I can slide back to the sign in view. I don't want that. How can I segue to the main view without having access to going back unless the user taps the sign out button and then goes back to the home view without again having access to go "back" to the main view?
For gesture recogniser to swipe back to previous page.
self.navigationController.interactivePopGestureRecognizer.enabled = false
If you have a navigation bar, you might need to hide it too. Else you can also hide the back button by:
self.navigationController.navigationItem.backBarButtonItem.enabled = false

Segue from modal view to tab bar view controller and not lose tab bar

Hello Im trying to segue from a modal to a tab bar view controller without losing the tab bar? I know the question is short, but this is all I'm asking.
Scenario: I have a Tab Bar View Controllers, A and B. B modals to C view controller. Then I want to return to View Controller A.
Swift please :D
Here is my example of how to do this. In my setup, I choose the yellow ViewController from the tab, then press Go! which modally presents the white ViewController. Pressing Exit returns to the green ViewController.
To set this up, use an unwind segue to return to the viewController that called you. For instance, implement this in the first ViewController of the tab (the one calling the modal segue).
#IBAction func backFromModal(_ segue: UIStoryboardSegue) {
print("and we are back")
// Switch to the second tab (tabs are numbered 0, 1, 2)
self.tabBarController?.selectedIndex = 1
}
Then switch to another tab using self.tabBarController?.selectedIndex = n where n is the number of the tab you really want to go to. To set up the unwind segue, you can either control-drag from a button in your modal view controller to the exit icon at the top of the viewController and select backFromModal from the pop up...
OR
you can set up the unwind segue to be called programmatically by control-dragging from the viewController icon at the top of the modal viewController to the exit icon, and select backFromModal from the pop up.
Then, go to the Document Outline View and click on the unwind segue
and give it an identifier in the Attributes Inspector on the right (for example "returnFromModal").
Then you'd call the unwind segue like this:
self.performSegue(withIdentifier: "returnFromModal", sender: self)

Navigation bar object in xcode 5 not showing back button and has no functionality

I have been trying to properly setup a navigation bar in one of my View Controllers for an hour now and have not found any working solutions.
I control-clicked on a button on my app's initial view controller(1st VC) and dragged to another view controller(2nd VC) and selected "modal" as the action segue.
I then added a navigation bar item to my 2nd view controller.
When I run my app on my iPhone, I can tap on the button on my app's initial screen and it will take me to my 2nd VC, and the 2nd VC does display the navigation bar, but the navigation bar does not have the default iOS 7 back arrow to let me go back to the app's initial VC.
I was under the impression that this could be setup exactly like I did above and that the back button functionality would be included by default.
Am I completely lost? Do I need to further customize navigation bar programmatically or with a tick box in the attributes inspector? Is "modal" the wrong action segue option?
I basically just want to have navigation bars at the top of a couple of my VC's so that the user can easily get back to the app's initial screen.
Thanks for the help.
Since you are presenting your second screen (2nd VC) as MODAL from your first screen (1st VC), you will not see the back arrow button on navigation bar. Your understanding about back button works for Navigation view controllers (push segue). For MODAL you need to put a cancel button on second VC's Nav bar and put a dismiss action for that.

Modal view controller hides tab bar

In my tab based app, on clicking one of the tabs, I want to display a modal view controller with some info.
In my app delegate's didSelectViewController method, I am adding modal view. But it occupies entire screen and hides the tab bar. I don't want to hide the tab-bar, just want to display modal-view which pops up and can be dismissed.
How do I do it?
Please help.
Thanks in advance.
Modal view controllers are always presented full screen on an iPhone. If you don't want to hide the tab bar, then you need to present this view in some other way besides modal.
I was able to "present" a new view controller, over another, UNDER the tab bar, by setting modalPresentationStyle to .currentContext.
let newViewController = UIViewController()
newViewController.view.backgroundColor = UIColor.red
newViewController.modalPresentationStyle = .currentContext
newViewController.modalTransitionStyle = UIModalTransitionStyle.flipHorizontal
present(newViewController, animated: true, completion: nil)
Edit: From more testing, the above can have some buggy behavior if someone changes the tab WHILE the newViewController is presented.
To "fix," I created a "switcher" - a UIViewController that animates between the view controllers that I want to flip UNDER the tab bar:
view.addSubview(nextView)
UIView.transition(from: currentView,
to: nextView,
duration: 0.5,
options: animation,
completion: { (_) in
currentView.removeFromSuperview()
})
In this case, currentView is the view of ViewControllerOne (the currently visible one), and the nextView is view of ViewControllerTwo (the one we want to present).
For example if secondViewController is the second viewController for your second tabbar, you should do like this:
[secondViewController.view addSubview:theViewYouWantToShow];
In iOS , Modal View Controller is always have highest priority in all view controllers available. So you can not use Modal View Controller in your case.
If you just want to show popup on the screen with back ground visibility, then just use UIAlertView. You can add OK or CANCEL button as per your requirement to remove alert view.
Or
If you want to show a full view with tab bar visibility, then add the view in a that tab as a subview. You can give it a feel like a pop up using transform property of a view.
You can present it modally by setting PresentationStyle.This style presents viewController in a square and it does not occupy full screen.
self.modalPresentationStyle = UIModalPresentationFormSheet;
You can also set the transition:
self.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;