objective-c - Cocoa-Touch – 委托(delegate)混淆

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

我刚刚启动了一个运行 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/

相关文章:

c# - 非常简单的委托(delegate)沉思

ios - 如何使用 Objective-C NSArray 的 getter 和 setter 定义 Swift 协议(protocol)

iphone - 使用一个委托(delegate)来管理两个 UIActionSheet

iphone - 从 Settings.bundle 中的 MultiValueSpecifier 首选项访问标题和值

ios - 在通过 NavigationControllerDelegate 进行交互式转换期间隐藏/取消隐藏导航栏的正确方法是什么?

ios - 在使用委托(delegate)的 ObjectiveC 中,如何在不使用 UIButton 的情况下发送消息?

iphone - 为什么委托(delegate)声明前面没有*?

ios - 从 iOS7 中的应用程序中关闭所有 UIAlertview

iphone - 获取两个位置之间的路线方向

iphone - 无法识别的选择器发送到 UIViewController 上的实例