So I've created MainViewController which is a subclass of UITabBarController:
#interface MainViewController: UITabBarController {
}
I initialized this from the app delegate and then set delegate to self:
MainViewController * main = [[MainViewController alloc] init];
main.delegate = self
then I had:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
}
but this was never called.. why is this? Is this because this was a subclass?
Just to clarify: do you have
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
in MainViewController, or in your app delegate?
That method should be defined in whichever class you choose to be your UITabBarControllerDelegate. For example, in my app I have:
A regular UITabBarController (there's no need to subclass UITabBarController unless you're doing something fancy)
My app delegate implements UITabBarControllerDelegate - specifically, tabBarController:shouldSelectViewController:
I set tabBarController.delegate to be the app delegate
That should be everything you need.
Related
I have two view controllers (FirstViewController and SecondViewController) and a Tab Bar Controller and I'm using Storyboards. In the FirstViewController user can drag and drop an imageview. So every time a user clicks on the second TabBarItem which displays the SecondViewController I would like to check if the user has dropped the image or not every time she clicks the TabBarItem.
So I understand that this can be done with UITabBarDelegate and with its method -(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item. But I'm doing something wrong because the method isn't called and I believe this is because I can't set the delegate properly. So I want the SecondViewController to be the delegate for TabBarController.
So in my SecondViewController.h I have the following
#interface SecondViewController : UIViewController<UITabBarDelegate>
And in SecondViewController.m I have
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(#"%#", item);
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tabBarController.delegate = self;
}
But nothing happens and when setting the delegate I also get a compiler warning: Assigning to 'id' from incompatible type 'SecondViewController *const __strong'
Please be gentle with me, this is my first app and the first time I'm trying to use delegates.
Add the following code to any of the view controllers
UITabBarController *tabBarController = (UITabBarController*)[UIApplication sharedApplication].keyWindow.rootViewController ;
[tabBarController setDelegate:self];
// add any delegates methods to your class
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(#"%#", tabBarController);
}
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
This method is a delegate method for UITabBar, not UITabBarController, so
self.tabBarController.delegate = self;
will not work.
Tab bar controller has its own UITabBar, but changing the delegate of a tab bar managed by a tab bar controller is not allowed, so just try UITabBarControllerDelegate method like this:
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
NSLog(#"%#", item);
}
For more detail check info
Thanks
I imported and implemented the following. Hope it helps.
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if (_mainTab.selectedItem.tag == 1) {
NSLog(#"TAB 1");
}
else if (_mainTab.selectedItem.tag == 2) {
NSLog(#"TAB2");
}
else if (_mainTab.selectedItem.tag == 3)
{
NSLog(#"TAB3");
}
else
{
NSLog(#"TAB NOT WORKING");
}
}
You are using the wrong delegate protocol UITabBarDelegate is usually used for customizing the UITabBar objects. You need to use UITabBarControllerDelegate protocol in order to check if a tab is selected or customize the behavior of tabs.
You should implement UITabBarControllerDelegate protocol instead and use this delegates callback to track selection:
tabBarController:didSelectViewController:
Next thing is, that you should initialize delegate before it will be called. ViewDidLoad will be called after tabbarcontroller will try to talk to delegate.
In order to get rid of the compiler warning your SecondViewController should conform to the UITabBarControllerDelegate protocol instead of the UITabBarDelegate protocol.
#interface SecondViewController : UIViewController<UITabBarControllerDelegate>
I think I've done my homework here.
I want my app delegate to be the delegate for my UITabBarController.
Using IB, I've connected UITabBarController's delegate to my Application Delegate.
App Delegate Header file is:
#interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
The delegate method I'm trying to implement is:
-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
NSLog(#"shouldSelectViewController called.");
// do some stuff with viewController
return YES;
}
My app delegate has an outlet to the UITabBarController that's connected in IB. When I do this:
NSLog(#"tab bar controller delegate is %#", self.tabBarController.delegate);
I get a good result such as tab bar controller delegate is <MyAppDelegate: 0x6e86a30>.
What am I missing?
Just write this code. Usually in viewDidLoad().
self.tabBarController.delegate = self;
If the current controller is a UITabBarController then:
self.delegate = self
Ok, found the solution. I had some old code in my RootViewController that set up this controller as the delegate. No delegate methods were implemented on the RootViewController, so it appeared as if nothing was happening. Because the RootViewController is set as delegate AFTER MyAppDelegate, the delegate was actually set to the RootViewController.
So the lesson is double-check your code to make sure some other object isn't also being set as the delegate.
How can i get the index of the tabBar when i touch a tab?
I´ve extended my class to < UITabBarController > and added the method
- (void)tabBarController:(UITabBarController *)tabBarController
didSelectViewController:(UIViewController *)viewController
{
if (viewController == tabBarController.moreNavigationController)
{
tabBarController.moreNavigationController.delegate = self;
}
}
But that method isn´t triggered when i touch a tab item.
What should i do?
UITabBarController has a property selectedIndex that you can use to find the selected tab.
More information about the UITabBarController can be found here
Also be sure to set the delegate property of your UITabBarController, otherwise the delegate messages (such as didSelectViewController:) will not be received.
Did you set the delegate of the UITabBarController to your delegate class when you create it?
I have a UITabBarController instansiated in my appDelegate, but I need a way to know when the user presses the different tab bar items (buttons on the tab bar).
-UITabBarDelegate protocol to the rescue (with the required didSelectViewController-method)!
With everything wired up in Interface Builder, how do I obtain a reference to the actual UIViewController subclass instance that corresponds to this tab bar item that was pressed?
I need this reference because I need to call a method in one of my UIViewControllers subclasses every time that tab bar item is pressed.
Any suggestions?
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(#"%#", [[self.tabBarController selectedViewController] nibName]); // nil, no success here
if ([theTabBarController selectedIndex] == 1) {
MySecondViewController *reference = (MySecondViewController *) viewController;
if ([reference isKindOfClass:[UINavigationController class]]) {
NSLog(#"OMG. It's a UINavigationController class??!"); // kicks in for some reason, shouldn't reference be a MySecondViewController
}
}
Possibly I am not understanding your question correctly, but it seems that what you're asking for is simply the "viewController" parameter which is passed into the method call you've mentioned
- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
UITabBarController also has a property to get the same info
#property(nonatomic, assign) UIViewController *selectedViewController
i have implemented Tab bar controller in IB.but when i write the following method in appdelegate.m , it is not called(i have put break point) when i tab ...?any help?
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
Wherever you create the UITabBarController, you need to set its delegate property to the custom UITabBarControllerDelegate object you've created.
If you've extended your application delegate to implement the methods of the UITabBarContollerDelegate protocol, and create the UITabBarController in the application delegate, then add the following after creating it:
tabBarVaribleName.delegate = self;