【问题标题】:Cocoa-Touch – Delegate confusionCocoa-Touch – 代表混乱
【发布时间】:2012-02-07 20:38:27
【问题描述】:

我刚刚开始了一个运行 Xcode 4.2.1 和 iOS5 SDK 的新项目。该项目是使用 ARC 设置的。我正在尝试通过执行 [tabBarController setDelegate:self]; 将 AppDelegate 设置为 UITabBarController 的代表,如果我这样做,我会收到一条警告消息:

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

好吧,我设置了我的 AppDelegate 以符合 UITabBarControllerDelegate

@interface AppDelegate : UIResponder <UIApplicationDelegate, UITabBarControllerDelegate>

很好,警告消失了。

我现在遇到另一个错误。在视图控制器中,我想获取 AppDelegate,所以我这样做:AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; 但这会显示警告:

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

但如果我删除我的 AppDelegate 符合 UITabControllerDelegate 协议,我的第二个警告就会消失。

很奇怪的行为,Cocoa 专家有什么用?

【问题讨论】:

    标签: objective-c cocoa-touch delegates automatic-ref-counting uiapplicationdelegate


    【解决方案1】:

    在分配 AppDelegate 变量之前尝试进行类型转换。

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

    并且,保留UITabControllerDelegate

    【讨论】:

    • 嗨@Aadhira,这解决了我的错误,但我不确定我是否理解该解决方案为何有效。你或任何人介意向我解释一下吗?谢谢!
    • stackoverflow.com/questions/14977284/… 请参考以上链接-@narner
    猜你喜欢
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    相关资源
    最近更新 更多