我刚刚启动了一个运行 Xcode 4.2.1 和 iOS5 SDK 的新项目。该项目是使用 ARC 设置的。我正在尝试通过执行 [tabBarController setDelegate:self];
将 AppDelegate 设置为 UITabBarController 的委托(delegate);如果我这样做,我会收到一条警告消息:
warning: Semantic Issue: Sending 'AppDelegate *const __strong' to parameter of incompatible type 'id<UITabBarControllerDelegate>'
好吧,我设置我的 AppDelegate 以符合 UITabBarControllerDelegate
@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>
很好,警告消失了。
我现在遇到另一个错误。在 View Controller 中,我想获取 AppDelegate,所以我这样做:AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
但这将呈现一条警告:
warning: Semantic Issue: Initializing 'AppDelegate *__strong' with an expression of incompatible type 'id<UIApplicationDelegate>'
但是如果我删除我的 AppDelegate 符合 UITabControllerDelegate 协议(protocol),我的第二个警告就会消失。
非常奇怪的行为,是什么给了 Cocoa 专家?
最佳答案
在分配 AppDelegate 变量之前尝试进行类型转换。
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
并且,保留 UITabControllerDelegate
。
关于objective-c - Cocoa-Touch – 委托(delegate)混淆,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8820137/