Unselect or deselect all tabs in a tab in iOS 5
I am new to iOS development and I started with IOS 5 directly. I have created a storyboard which consists of a tab controller as its rootviewcontroller. I added two tabs.
I want to deselect / deselect all tabs first. How should I do it? I have tried the following
UIView *view = [[UIView alloc]initWithNibName:@"view" bundle:[NSBundle mainBundle]];
[self.tabBarController setSelectedViewController:nil];
[self.tabBarController setSelectedViewController:view];
where i added a view with id "view".
But it didn't work, it gives an error:
unrecognized selector sent to instance
I have also tried the following
[self.tabBarController.tabBar setSelectedItem:nil];
but he says
'NSInternalInconsistencyException', Reason: "Incorrect modification of the tab bar controlled by the tab bar controller is not allowed."
I tried this code in the controller for the first tab. I want to do this because I want to put the default view on top of the first tab view and hide it as soon as any of the tabs below is clicked.
I use this to clear any selected tabBarItems
[myTabBar setSelectedItem:nil];
Old question. For record only, you cannot deselect all tabs if the tab bar is controlled by a TabBarController. Method:
[self.tabBar setSelectedItem:nil];
Only works if the tab bar is not controlled from the tab bar controller. If so, there is no way to deselect all tabs, each must be selected.
It's a bit hanky. But I just set the tint color in -viewWillAppear to gray when I wanted it to look like there was no selection, and then set the tint color back to the "selected" color when I want to show the selection.
// since we cant turn off tabbar selection, change the color to make
// it look like nothing was selected.
self.tabBarController.tabBar.tintColor = [UIColor grayColor];
-----------
// make sure tintcolor is turned on (just in case another module turned it off
self.tabBarController.tabBar.tintColor = self.view.tintColor;
I found a solution that is probably not the best way to do it, but it worked for me ([self.tabBar setSelectedItem: nil]; was not). I put my view as a parent view (myTabBarViewContainer), and then when I want to deselect my items, I call removeFromSuperview on the parent view and restart it:
[self.myTabBarViewContainer.view removeFromSuperview];
Then restart it and Voila!
Not very pretty, but it works ...
You need to use UITabBar. Not UITabBarController
unselect or deselect a swift 3.0 tab item
tabbar_Main.selectedItem = nil