ios - 尝试创建协议(protocol),获取时找不到协议(protocol)声明

标签 ios objective-c delegates uialertview

我正在创建类似 UIAlertView 的东西。 我试图创建它接近苹果所做的,所以我创建了一个名为 MyAlertView 的类并创建了一个名为 MyAlertViewDelegate 的协议(protocol),我在该类中有一个名为 delegate 的属性,但我得到了

cannot find protocol declaration

完整代码如下:

#import <UIKit/UIKit.h>
#import "AlertView.h"


@interface MyAlertView : NSObject
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id /*<UIAlertViewDelegate>*/)delegate cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... NS_REQUIRES_NIL_TERMINATION;

@property(nonatomic,assign) id<MyAlertViewDelegate> delegate;    // weak reference
@property(nonatomic,copy) NSString *title;
@property(nonatomic,copy) NSString *message;   // secondary explanation text
@property(nonatomic,strong) UIWindow *myWindow;
@property(nonatomic,strong) AlertView *alertView;

// adds a button with the title. returns the index (0 based) of where it was added. buttons are displayed in the order added except for the
// cancel button which will be positioned based on HI requirements. buttons cannot be customized.
- (NSInteger)addButtonWithTitle:(NSString *)title;    // returns index of button. 0 based.
- (NSString *)buttonTitleAtIndex:(NSInteger)buttonIndex;
@property(nonatomic,readonly) NSInteger numberOfButtons;
@property(nonatomic) NSInteger cancelButtonIndex;      // if the delegate does not implement -alertViewCancel:, we pretend this button was clicked on. default is -1

@property(nonatomic,readonly) NSInteger firstOtherButtonIndex;  // -1 if no otherButtonTitles or initWithTitle:... not used
@property(nonatomic,readonly,getter=isVisible) BOOL visible;

// shows popup alert animated.
- (void)show;

// hides alert sheet or popup. use this method when you need to explicitly dismiss the alert.
// it does not need to be called if the user presses on a button
- (void)dismissWithClickedButtonIndex:(NSInteger)buttonIndex animated:(BOOL)animated;

// Alert view style - defaults to UIAlertViewStyleDefault
@property(nonatomic,assign) UIAlertViewStyle alertViewStyle NS_AVAILABLE_IOS(5_0);

/* Retrieve a text field at an index - raises NSRangeException when textFieldIndex is out-of-bounds.
 The field at index 0 will be the first text field (the single field or the login field), the field at index 1 will be the password field. */
- (UITextField *)textFieldAtIndex:(NSInteger)textFieldIndex NS_AVAILABLE_IOS(5_0);

@end

@protocol MyAlertViewDelegate <NSObject>
@optional

// Called when a button is clicked. The view will be automatically dismissed after this call returns
- (void)alertView:(MyAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex;

// Called when we cancel a view (eg. the user clicks the Home button). This is not called when the user clicks the cancel button.
// If not defined in the delegate, we simulate a click in the cancel button
- (void)alertViewCancel:(MyAlertView *)alertView;

//- (void)willPresentAlertView:(NSString *)alertView;  // before animation and showing view
- (void)didPresentAlertView:(MyAlertView *)alertView;  // after animation

- (void)alertView:(MyAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex; // before animation and hiding view
- (void)alertView:(MyAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;  // after animation

// Called after edits in any of the default fields added by the style
- (BOOL)alertViewShouldEnableFirstOtherButton:(MyAlertView *)alertView;

@end

在这一行:@property(nonatomic,assign) id<MyAlertViewDelegate> delegate; // weak reference我得到错误

 Cannot find protocol declaration for 'MyAlertViewDelegate'; did you mean 'UIAlertViewDelegate'?

它怎么知道关于 UIAlertViewDelegate 的任何事情?为什么它不能识别我自己的协议(protocol)?

最佳答案

您必须在 @interface 声明上方添加 @protocol MyAlertViewDelegate,例如:

@protocol MyAlertViewDelegate;

@interface MyAlertView : NSObject

关于ios - 尝试创建协议(protocol),获取时找不到协议(protocol)声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19313701/

相关文章:

ios - TableViewController 在搜索时填充,但不会加载初始 View

ios - 以编程方式隐藏键盘快捷键栏iOS 13

iphone - MKMapView使用名称在国家/地区上放置图钉

Activity中的Java类调用方法

c# - 委托(delegate)可以有可选参数吗?

ios - 根据来源不同,Unwind Segue 也不同

ios - 如何从委托(delegate)在线程内构建我的单元格?

iphone - Objective-C 代码中未找到 windowScriptObject 方法

ios - 调试和发布配置之间的内存使用差异

delegates - 如何将表达式树保存为新的可执行磁盘文件的主要入口点?