Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa-Touch – Delegate confusion

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?

like image 617
Peter Warbo Avatar asked Jan 11 '12 13:01

Peter Warbo


1 Answers

Try doing a typecast before assign the AppDelegate variable.

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

And , keep the UITabControllerDelegate.

like image 187
Ilanchezhian Avatar answered Sep 24 '22 20:09

Ilanchezhian