I have 3 viewcontroller in tabbar. I am loading tabbar from appdelegate. Now I want to make an event when user change viewcontroller from tabbar. For that I have wrote delegate method didselectitem in appdelegate and set delegate in my viewcontroller. But delegate method is not called.
In my viewcontroller I am writing this:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
NSLog(#"tabbar is %#",tabBar.selectedItem);
}
in viewdidload:
MyAppDelegate.tabBar.delegate=self;
Make sure to set the delegate of the UITabBarController and not the tab bar itself. Also, make sure to implement the correct delegate method: tabBarController:didSelectViewController:
More information in the UITabBarControllerDelegate documentation.
Related
I am NOT using a UITabBarController.
I have a view controller and I added a UITabBar, it has two items.
How can I make sure that when the first item is clicked, it loads the content from a specific view controller, and when another item is clicked, loads the content from another view controller?
As mentioned in other answers (Detect when a tab bar item is pressed), You should implement the UITabBarDelegate protocol.
Once you're implementing this protocol, just use the
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item method to know when a user changes the selected item of a tab bar. implement this method, and you will have to check which item is selected, and manually change your view's content based on that.
You need to set the delegate of the UITabBar to be the UIViewController and the UIViewController needs to conform to the UITabBarDelegate protocol and implement the method
Swift
optional func tabBar(_ tabBar: UITabBar, didEndCustomizingItems items: [UITabBarItem], changed changed: Bool)
Objective-C
- (void)tabBar:(UITabBar *)tabBar didEndCustomizingItems:(NSArray<UITabBarItem *> *)items changed:(BOOL)changed
You can find details in the UITabBarDelegate documentation
I Want to call my own method, i.e. myMethod() when same tab bar is selected which is already selected.
I had tried to call didSelectMethod() but it is not called in my ViewController class.
Please help me in this,
Thank you
Jagveer Rana
Where you own your tabBarController instance there you set delegate
tabBarController.delegate= self; // It is must to set delegate
add the below delegate method
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
if ([viewController respondsToSelector:#selector(myMethod)]) {
[viewController performSelector:#selector(myMethod)];
}
}
It sounds like your ViewController class is not the delegate for your UITabBarController, otherwise tabBarController:didSelectViewController: would be called.
Make sure your delegate is linked up properly.
If that's not the problem, then there are a few other StackOverflow questions asking the same thing:
Tab bar, reload every time tab is pressed
Detect a re-tab on selected UITabbarItem
Hi I am developing a UITabBar application.
I need:
1.When I select a TabBAr item I need to get the current selected index.
2.Based on the current index I want to perform some action in AppDelegate.
3.So is there a any delegate methods which called whenever I pressed the tab bar items.
4.Please suggest some way to check in AppDelegate every time I presses Tab Items
Thank You.
After a quick google:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
Make sure to link the delegate in IB...
Yes you can using this delegate method
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(#"selected index : %i",[tabBarController selectedIndex]);
}
this method called whenever you touch any of TabBarItem.
First set the delegate Protocol e.g UITabBarControllerDelegate and then
put self.tabbarcontroller.delegate = self; in your ViewDidLoad or somewhere where the object get's initialized
Firstly, I have set both viewcontrollers to be UITabBarController delegates. Both are part of a tab bar controller. I did this by putting the following code into each viewDidLoad:
self.tabBarController.delegate = self;
Then I added the following delegate method to CalculatorsViewController:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[self presentCalculatorsView];
}
Where presentCalculators view simply reveals a subview within the same view controller.
I also added the following delegate method to the OptionsViewController:
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
[self presentHomeScreen];
}
Again this method simply reveals another subview within the viewController.
The problem I am having is that the OptionsViewController presentHomeScreen method is only called if I do not visit the CalculatorsViewController. Once I do visit the CalculatorsViewController in the app and then return to OptionsViewController,
[self presentHomeScreen]
is never called. In fact, it appears that it still calls the method from the CalculatorsViewController. I tested it with an NSLog statement.
Any ideas why one method overrides the other? Or why the tab bar button executes code from another viewController, other than the one that is active?
EDIT* It is almost as if the one viewController 'steals' the delegate from the other.
By calling self.tabBarController.delegate = self; on each viewDidLoad method, you are basically telling the tab bar controller to use abandon the current delegate and use the current view controller as delegate.
Note that the viewDidLoad method is called only once under normal circumstances. (It may be called again when the view of your view controller is unloaded due to memory warning, for example, then you access the view of your view controller again, which calls loadView/awakeFromNib and viewDidLoad. I'm not entirely sure on this scenario though.) In your scenario:
Open OptionsViewController for the first time - tab bar controller's delegate is OptionsViewController
Open CalculatorsViewController for the first time - tab bar controller's delegate is now CalculatorsViewController
Go back to OptionsViewController - tab bar controller's delegate is still CalculatorsViewController, as the viewDidLoad is not called again
If you must change the delegate, you can do it instead in the viewWillAppear method.
I am trying to find a way to make an item of the TabBar acting as a "UIbutton".
I would like when pressing to this item just make it work as a ibaction method.
I tried several implementation as :
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{
}
But I was not able to get the method working.
What will be the best solution to fake an item function of a tabbar, and make it open an UIActionSheet for example ?
Try using an IBOutlet and UITabBarDelegate in your viewController.
#interface MyViewController : UIViewController <UITabBarDelegate> {
IBOutlet UITabBar* tabBar;
}
#end
Connect the UITabBar to that IBOutlet in Interface Builder.
Set the delegate to 'self' in your viewController's viewDidLoad method:
-(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
tabBar.delegate = self;
}
And set up the delegate method with some way to differentiate the tabs:
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSLog(item.title);
if([item.title isEqualToString:#"some label"]) {
// do something for this specific button
}
}
(This is an old post, but I had the same question. Hopefully this saves someone else the same trouble.)
You can try the tabBarController shouldSelectViewController delegate method. Override this. Return NO on the tab you want to display an ActionSheet from, and instead instantiate the ActionSheet in this method.
You'll be required to have a placeholder UIViewController in place of this tab.
As mentioned above, this goes against Apple HIG and might be reason for your app to be rejected.
Check this out! These people create a tabBar with a uibutton in it! http://idevrecipes.com/2010/12/16/raised-center-tab-bar-button/
Full source code:
https://github.com/boctor/idev-recipes/tree/master/RaisedCenterTabBar
I tried the code some time ago and it worked for me.
Call your action method in the viewDidLoad method of the view of the tab bar item selected.