iphone - 如何使用可选方法创建协议(protocol)?

我注意到在 iPhone SDK 中定义的几个协议(protocol)中标记为可选的方法,例如 UIActionSheetDelegate 协议(protocol)。

如何定义自己的协议(protocol),并将其中一些方法设置为可选?

最佳答案

来自“Formal Protocols”上的 Apple 页面:

Optional Protocol methods can be marked as optional using the @optional keyword. Corresponding to the @optional modal keyword, there is a @required keyword to formally denote the semantics of the default behavior. You can use @optional and @required to partition your protocol into sections as you see fit. If you do not specify any keyword, the default is @required.

@protocol MyProtocol

- (void)requiredMethod;

@optional
- (void)anOptionalMethod;
- (void)anotherOptionalMethod;

@required
- (void)anotherRequiredMethod;

@end

https://stackoverflow.com/questions/322498/

相关文章:

ios - UITableView 设置为静态单元格。是否可以以编程方式隐藏某些单元格?

ios - Objective-C中的方法重载?

ios - iPhone/iPad的UILabel如何设置粗体和斜体?

ios - 如何在 UIView 的顶部添加边框

ios - 在 iPhone App 中如何检测设备的屏幕分辨率

objective-c - 是否可以在 Obj-C 中使用 Swift 的枚举?

iphone - iOS:如何执行 HTTP POST 请求?

objective-c - 错误 : writable atomic property cannot

objective-c - 在 Objective-C 中加入一个数组

ios - 更改 UITableView 部分标题的字体大小