6

I have a tabbar based application and do the following to get a reference to the application delegate:

MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

Which then gives this warning:

warning: type 'id <UIApplicationDelegate>' does not conform to the 'UITabBarControllerDelegate' 

My application delegate header looks like this:

#import <UIKit/UIKit.h>

@interface MyAppDelegate : NSObject <UIApplicationDelegate, UITabBarControllerDelegate>     {
UIWindow *window;
UITabBarController *tabBarController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;
@property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;

@end

The only methods in the .m file are applicationDidFinishLaunching and dealloc. What else do I need to conform to the protocol?

2
  • 1
    have you tried looking up the UITabBarDelegate protocol's documentation?
    – Jasarien
    Feb 6, 2010 at 20:36
  • Yep - I've noticed Apple doesn't tell you which of the protocol methods are required. Unless I missed that.
    – 4thSpace
    Feb 6, 2010 at 22:14

4 Answers 4

14

It's a static warning. It means that the return type of [[UIApplication sharedApplication] delegate] does not conform to the tab bar delegate protocol, which is true.

Cast the value returned from [[UIApplication sharedApplication] delegate] to get rid of the warning:

MyAppDelegate *appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
1
  • My opinion is that the "checked" answer to this is incorrect. I do not have enough "points" to vote it down...but casting to hide a deficiency in your implementation can come back and bite you. My vote was for "Jasarien". Conform to the UITabBarDelegate and you will not have the warning anymore.
    – Jann
    Feb 7, 2010 at 5:35
2

If you declare MyAppDelegate as conforming to UITabBarDelegate, using <UITabBarDelegate>, then you need to implement at least the required methods of the protocol.

You should read up on how protocols work, and the differences between formal and informal protocols.

The method you need to implement to conform to the UITabBarDelegate is

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
1

I would like to chime in because the second and third comments are technically incorrect. The error says that the AppDelegate does not conform to the UITabBarController delegate. The answers about mention the UITabBarDelegate.

Apples and Oranges.

Incidentally, the Apple sample code uses the casting method as well.

1
  • I suspect you misread the original past. Both the error and code snippet say "UITabBarControllerDelegate". That last word 'Delegate' may have been scrolled off the edge of StackOverflow's display so you did not see it. Feb 5, 2013 at 8:30
0

"Incidentally, the Apple sample code uses the casting method as well." - Jeff I think that it is not incidentally. Error comes, because delegate property is id "UIApplicationDelegate", however one obtains warning because he assign it to MyAppDelegate, which is not only conforms to UIApplicationDelegate but also UITabBarControllerDelegate. That is why, if one introduces cast
MyAppDelegate appDelegate = (MyAppDelegate)[[UIApplication sharedApplication] delegate]; it works properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.