App Delegate发出警告:'id <UIApplicationDelegate>'不符合'UIAlertViewDelegate'协议

W D*_*son 0 iphone uialertview uiapplicationdelegate ios4

我正在使用UIAlertViewDelegate在我的应用程序启动时使用UIAlertView.一切正常.唯一的问题是,每当我引用App Delegate时,我都会收到警告"type'id'不符合'UIAlertViewDelegate'协议",给出了大约32个警告.

谁能解释为什么我会收到这个警告以及我如何满足它?

提前致谢!

Fir*_*eer 6

我假设你的app delegate是你的警报视图委托?

如果是这样,您需要告诉编译器在app委托的声明中.像这样:

@interface SomeAppDelegate : NSObject <UIApplicationDelegate, UIAlertViewDelegate> {

}

// ... rest of interface

@end
Run Code Online (Sandbox Code Playgroud)

然后您还需要实现所需的方法.

编辑:进一步思考,我想你可能会在引用应用代表时错过演员.所以你有这样的事情:

MyAppDelegate* appDelegate = [[UIApplication sharedApplication] delegate];

// what you want is:

MyAppDelegate* appDelegate = (MyAppDelegate*)[[UIApplication sharedApplication] delegate];
Run Code Online (Sandbox Code Playgroud)

  • 是的,就像大多数事情一样,今天早上我比昨晚要清楚得多.:)所以'appDelegate'被声明为MyAppDelegate*,这意味着必须满足<UIApplicationDelegate> AND <UIAlertViewDelegate>.但是UIApplication中的属性"委托"被声明为类型id <UIApplicationDelegate>.所以编译器只是警告你这个id <UIApplicationDelegate>类型不满足<UIAlertViewDelegate>.演员说,不,代表实际上是MyAppDelegate*. (2认同)