Cocoa-Touch – Delegate confusion

Peter Warbo picture Peter Warbo · Jan 11, 2012 · Viewed 9.9k times · Source

I just started a new project running Xcode 4.2.1 and iOS5 SDK. The project is setup with ARC. I'm trying to set the AppDelegate to be the delegate for UITabBarController by doing [tabBarController setDelegate:self]; if I do that I get a warning message saying:

warning: Semantic Issue: Sending 'AppDelegate *const __strong' to parameter of incompatible type 'id<UITabBarControllerDelegate>'

Alright fair enough, I set my AppDelegate to conform to the UITabBarControllerDelegate by doing

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

Great, the warning goes away.

I now get another error. In a view controller I want to get a hold of the AppDelegate so I do this: AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; but this will render a warning saying:

warning: Semantic Issue: Initializing 'AppDelegate *__strong' with an expression of incompatible type 'id<UIApplicationDelegate>'

But if I remove that my AppDelegate conforms to the UITabControllerDelegate protocol my second warning disappears.

Very strange behaviour, what gives Cocoa experts?

Answer

Ilanchezhian picture Ilanchezhian · Jan 11, 2012

Try doing a typecast before assign the AppDelegate variable.

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

And , keep the UITabControllerDelegate.