How to set first TabBar selected programmatically on iPhone - ios

I have UITabBar in view which have 5 tabs. I am using didSelectItem delegate to open different view i.e. I am NOT using TabBarController.
My problem is on view load I need first tab get selected by default. Is there any property in TabBar which we can set to make it selected?
Thanks.

This code will work [tabBar setSelectedItem:[tabBar.items objectAtIndex:0]];

In swift if tabbar is used not tabbarcontroller set default select
var tabbar:CustomTabBar?//if declare like this
tabbar!.selectedItem = self.tabbar!.items![0] as? UITabBarItem
or
let tabbar = UITabBar()//if declare and initilize like this
tabbar.selectedItem = self.tabbar.items![0] as? UITabBarItem

set the
tabbar.selectedItem=0; in the viewWillAppear so when ever the view appears it will select the first tab by default.

[self.tabBar setSelectedItem:self.tabBar.items[0]];
or
self.tabBar.selectedItem = self.tabBar.items[0];
The selectedItem property requires a TabBarItem and not an index. So provide the tabbaritem in index 0 for the first tab.
This is wrong then: tabbar.selectedItem=0;
You may select other tabs as well. Happy coding

Related

Replace one ViewController in TabbarController with NavigationBar

I noticed in Whatsapp there are four tabs. If you disable the access to Contacts, the view in first tab, Favorites, will be replaced with the information view which will guide user to open the Settings.
I was trying to copy this function with following codes (in AppDelegate):
let gotoSettingsVC = UIApplication.sharedApplication().keyWindow?.rootViewController!.storyboard?.instantiateViewControllerWithIdentifier("gotoSettingsViewController") as! GotoSettingsViewController
let tabbarController = UIApplication.sharedApplication().keyWindow?.rootViewController! as! UITabBarController
gotoSettingsVC.tabBarItem = tabbarController.tabBar.selectedItem
tabbarController.viewControllers![tabbarController.selectedIndex] = gotoSettingsVC
The gotoSettingsVC can be shown correct but without Navigation Bar. How to show this View with a Navigation Bar like the normal View in tabbarController?
thank you for any help.
Embed the GotoSettingsViewController in a navigation controller, and set that as one of the tabBar controllers view controller.

pass value to label inside UITabController from another tab iOS

I have UITabController with labels and buttons inside. I need to pass textView value from one tab to another's label. Using segues.
Simply I need to do like this code:
In TAB1:
textView1 = #"test";
set Tab2.label1 = textView1;
Thanks,
David.

Change Image of UItabbar Item, Using storyboards

I have the following story board:
As you can see there is a tab bar application with 5 tabs, on the storyboard I've assign the logo for each tab. Now when the user clicks a cell in a particular view I want to change the image of one of the tabs. How can I do this? I don't have an instance of the tab bar view controller or items since storyboards pretty much does all this for me. So my question is what methods do I have to implement to change the image? If I need the tab bar controller how can I get its instance and in which class should I point it to?
Thank you very much,
In any UIViewController class that is part of the Tab Bar hierarchy, all you have to do to get in instance of the tab bar controller is:
//In UIViewController
UITabBarController *tabBarController = self.tabBarController;
You can then change the image as so
//Suppose you want to change the 1st (0th) tab bar image
UITabBarItem * tabItem = [tabBarController.tabBar.items objectAtIndex: 0];
tabItem.image = //whatever image you want to change to
Each UIViewController has a property called tabBarItem which is a UITabBarItem that the tab bar controller uses to set the image representing that controller. You can manipulate that to change the image of the controller in question.
I found that -- at least in Xcode 6.1.1 using Swift -- the direct manipulation of the tabBarItem did not work for me.
However, #borrrden's answer put me on the right track. Apple's documentation for UITabBarController states pretty clearly:
You should never access the tab bar view of a tab bar controller
directly. To configure the tabs of a tab bar controller, you assign
the view controllers that provide the root view for each tab to the
viewControllers property.
...
Tab bar items are configured through their corresponding view
controller. To associate a tab bar item with a view controller, create
a new instance of the UITabBarItem class, configure it appropriately
for the view controller, and assign it to the view controller’s
tabBarItem property.
Therefore, in accordance with that, below is what I came up with that did work for me.
It's written in Swift, and I hope that future readers can translate it accordingly if they need to (I also changed the image names to be super-generic).
I also used UIImage's imageWithRenderingMode method so I could use custom images instead of the shadowy silhouette default images that iOS creates (I would like to credit #NSHeffalump's answer here for that...).
if let viewControllers = tabBarController.viewControllers as? Array<UIViewController> {
var tabBarItemImageNames = ["TabBarItemImage0","TabBarItemImage1","TabBarItemImage2","TabBarItemImage3","TabBarItemImage4"]
var vcIndex = 0
for vc:UIViewController in viewControllers {
let selectedImage = UIImage(named: tabBarItemImageNames[vcIndex])?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
let image = UIImage(named: tabBarItemImageNames[vcIndex])?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
var tabBarItem = UITabBarItem(title: "", image: image, selectedImage: selectedImage)
vc.tabBarItem = tabBarItem
vcIndex++
}
}

UITabbar nothing selected

U have got an UITabBar (not an controller) when I select an element, How can I change the UITabBar such that nothing is selected any more?
myTabbar.selectedItem = nil; // will remove selection

how can i set the badge in tab bar in objective-c?

I want to put the notification in the tab bar of the app to show the number of item selected for the shopping list.
Is there any way to add the notification in the tab bar of app.
You mean the badge? Do something like (where self is a UIViewController):
[[self tabBarItem] setBadgeValue:#"42"];
On the storyboard you have to select your navigation view controller if you have and then open your Property window and set Badge according to your requirement.
if you want to set programmatically
1. find your tabbar Item by viewcontroller
# badge=what you want to set.
2.[[[[self tabBarController] tabBar] items] objectAtIndex:1] setBadgeValue:badge];
and if you want to set by storyboard please refer this image.
Hope this will helps.
Thanks
Also if you want to use in swift. Below code will work for you
[[self tabBarItem] setBadgeValue:#"42"];
You can set the badgeValue property on a UITabBarItem to present the user with a small red badge on that item.
I did it using:
self.navigationController.tabBarItem.badgeValue = #"2";

Resources