Switch between views using a Tabbar - ios

I have an iOS application with a Tab Bar, and two subview. My first view is a Table View.
So, I want to switch to the second view when I click on a cell of the first view, and keep the TabBar visible.
When I do that using "Show" segue in the storyboard, I lost the TabBar.
And when I do it in my TableViewController with the following code, the second view is not loading.
tabBarController.selectedViewController = mySecondViewController
That only select the second element in the TabBar, but didn't display him.
Anyone have a solution ?

try using index of UITabBar item to switch, like this:
tabBarController.selectedIndex = 1
The problem also may be because you try to set View to selectedViewController, where uiviewcontroller should be. It is hard to say, because of lack of information.

In the story board make sure you control drag from the tab bar controller to your view controller and click "Relationship Segue: View Controller"

I've fix my problem. I've originally put the line of code bellow on the viewDidLoad() of my controller :
tabBarController.selectedViewController = mySecondViewController
After moving this line in a different methode, call by a simple button, that worked...

Related

How to use UItabbar on a UIviewcontroller?

currently I have a main Viewcontroller. Now I wish to implement the UItabbar to switch between few controllers. The problem now is this main view controller is just a normal UIviewcontroller, how can I implement the UItabbar and switch vc with selecting the tabbar item? Thanks all
if you are using storyboard
Get uitabbarcontroller-and link between it and the viewcontroller(by right click and drag to the view controller then select view controller from the drop menu)
Here is a tutorial
https://m.youtube.com/watch?v=nGx3MZM460c
If you want to create TabBar into your project you have to write it into AppDelegate:
let nav1 = UINavigationController(rootViewController: ViewController()) // ViewController inside TabBar
nav1.tabBarItem.title = "Title of VC"
nav1.tabBarItem.image = UIImage(named: "name_of_image")
let tabBarVC = UITabBarController()
tabBarVC.viewControllers = [nav1] // All VCs, what you want in TabBar
window?.rootViewController = tabBarVC
So I hope it helps you. If you write code programmatically without the storyboard.
through storyboard you can add tabBarcontroller to menuviewcontroller
intially my storyboard is like this
Drag from tabbarcontroller to menuviewcontroller you will get a pop up like this
Select relationship Segue "viewcontroller" and your storyboard will look like this
I don't know if its been made simpler since these answers were written. But it seems quite straightforward now - at least with Xcode 11.5.
So to add a tab bar to an existing Storyboard that's already got a UIViewController:
Drop a 'container view' onto your existing view. Pin it to the safe area. It doesn't need to cover the whole thing - eg. if you want a non-tabbed bit at the top. But it makes sense for it to extend it down to the bottom of the view - otherwise your tabs end up looking very non-standard!
When you dropped the container view, that will have created a new UIViewController for the container's contents. Delete it.
Now drop a Tab Bar Controller on the storyboard - eg. next to the main view controller's view. This creates three new view controllers - the tab bar controller, plus one for each of the two tab items it creates by default.
Now the crucial bit - we want to make the container view contain the tabbed views...
Select the Tab Bar Controller, select the Connections inspector, and drag from the 'Embed' presenting segue to the container on your main view.
Once you've dragged, a menu will pop up by the cursor with a single item - 'viewDidLoad'. Make sure you select this. It can be a bit fiddly to do for some reason - but once you select it correctly you'll see the link from 'Embed' to 'Container View viewDidLoad' in the Tab View Controller's connections inspector.
Voila! - you're done!

Transition from UIViewController to TabBarViewController

I have been searching for a solution for 6 hours, and come up with nothing that applies to my situation. my storyboard follows the flow of:
TabBarViewController -> NavigationController ->TableViewController -> UIViewController (see picture below)
When the "Notify" button, in the last view controller (Stranger view controller), is clicked, I want to programmatically transition/segue from that View Controller(Stranger View Controller), to a different child of the TabBarViewController (it is the controller titled "Look Around" within the illustration).
Every time I perform a traditional segue:
option + drag segue from Stranger View Controller --> Look Around
View controller
Give segue an identifier
programmatically use self.performSegueWithIdentifier.
I get a transition. but the "Look Around" tab bar is gone in the storyboard, and in the simulator, once I hit "Notify", the view changes to the "look around" view, but the tab bar below does not reflect that change, as it still has the previous tab highlighted.
I have already handled all the other processing within the IBAction function that I need to handle. It is just a matter of, correctly, sending the user to a different view, once they have hit "Notify".
Any guidance on this would be greatly appreciated.
If you have any questions or concerns for me, please do not hesitate to ask. I am new at this, and am open to any help.
ViewController is a child of NavigationBar and NavigationBarController is a Child of TabBarController. So Segue is not required as it will disturb the flow.
Try programmatically
#IBAction func notifyButtonTapped(sender: AnyObject) {
tabBarController?.selectedIndex = 1
tabBarController?.tabBar.hidden = false
self.navigationController?.popToRootViewControllerAnimated(false)
}

Segue from one ViewController to another ViewController inside a Tab Bar Controller?

In the interface builder I have a UITabBarController and it is set as the initial view controller. From the tab bar controller, I have linked three independent ViewControllers; two UIViewController's and one UITableViewController. I have embedded all three of these views inside UINavigationController's as each of these views will eventually segue to a new view.
Interface Builder
Problem:
I now want to link one of the UIViewController's to the UITableViewController using a button to segue to the table view. This way I can pass information, i.e. func prepareForSegue(), to the table view. I want to maintain the tab bar controller at the bottom, however I do not want to have the ability to go back to the previous UIViewController from the current UITableViewController via a UIBarButtonItem at the the top of the view; That is what the tab bar at the bottom is for.
However every time I segue "Show" the table view (it is actually a segue to the table views navigation controller), the navigation bars at the top and the bottom of the table view disappear. Is there anyway to prevent this from happening?
I have also tried segue "Show" directly to the table view, in which case the tab bar is visible, but then it displays a "back" button at the top of the view to segue back to the sending UIViewController. I am hesitant about accepting a solution that would just hide the back button, because I feel I will run into problems down the road when I want to navigate to a detail view from the table view itself, since I would be bypassing the UITableViewController's UINavigationController.
Any solutions would be greatly appreciated. I have been trying to solve this problem for hours and I'm about to put my head through my computer screen. Also I thought about just using tabBarController?.selectedIndex on the button click to shift to the table view, and then passing the information using NSUserDefaults, but this is out of the question since I would be passing a custom object, and would have to encode and decode every custom field.
I you use a segue to get to it, as you say, you will still be using the UIViewController's UINavigationController which seems a bit messy. So I actually think selectedIndex is probably the best way to go as once you change to the UITableViewController you'll be in the correct navigation stack.
Instead of using NSUserDefaults, why not just reference the UITableView itself from the UIViewController, set the values you want, and then swap to it using self.tabBarController.selectedIndex.
So for your scenario above it, assuming the UITableViewContollrer is the third view in the UITabBarController, you would do something like the following:
Pass whatever you want into the UITabBarController by setting some pre-defined var in it. For example, if there was a String called saveMe in the UITableViewController, then do the following in the UIViewController:
let navController = self.tabBarController?.viewControllers![2] as! UINavigationController
let tableViewController = navController.viewControllers.first as! JarListTableViewController
tableViewController.saveMe = "Saved string here"
Swap to the UITableViewController using:
self.tabBarController?.selectedIndex = 2
The only issue with this is using selectedIndex won't perform a transition animation but not sure if you need this. This link could help if you do.

Adding toolbar to a ViewController in storyboard

I'm using storyboard to create an iPad app. On the main view I have a toolbar and a bar button item, let's call it "show". Then I have dragged a table view controller into the storyboard. I have also added a subclass of UITableViewController to the files and made the class of the dragged table view controller to be that subclass. And I made a popover segue from the "show" button to the table view controller. It works fine, meaning that when "show" pressed I see the popover showing the correct data that I set in the table view. What I cannot seem to figure out is how to put a toolbar on top of the table view in the popover. I took a step back and used a UIViewController instead of UITableViewController and still cannot add a toolbar by dragging it to the view. Any help will be appreciated.
I ended up putting the TableViewController within a NavigationController and the latter in a PopoverController, all in the code, without using IB. I found this an easier solution to get the toolbar than anything else that might work.

Open TabBar subview

I got 2 subviews in a TabBar app. I should go to the 1st view from the 2nd, but not with help of tabbar, but throught another button, which is on the 2nd view.
If i create simple segue (between bytton on the 2nd view and the 1st view), tabbar will think, that i am still on the 2nd view and the 2nd button on the tabbar still highlighted.
Do you know correct way to realise it?
You can try to use the UITabBarController.selectedViewController property instead of segue.
If you want to change your view without touching tababr controller
use this line of code where you want to use
self.tabBarController.selectedViewController=[self.tabBarController.viewControllers objectAtIndex:1];
using this line you can move the 2 tab of the tabcontroller without touching tabbar.

Resources