【问题标题】:Is it possible to extend an protocol like this?是否可以扩展这样的协议?
【发布时间】:2010-09-27 11:25:10
【问题描述】:

我需要在我的自定义 UITextField 子类的委托协议中添加一些方法。所以我把它放在标题中:

@protocol MyTextFieldDelegate (UITextFieldDelegate)
- (void)textfieldDidSomething:(UITextField*)textField;
@end

这样好吗?

【问题讨论】:

标签: iphone objective-c delegates protocols


【解决方案1】:

原则上我认为它看起来不错。我要说的唯一一点是我会写这样的东西:

@protocol MyTextFieldDelegate (MyTextFieldDelegateExtras)
- (void)textfieldDidSomething:(UITextField*)textField;
@end

将其与UITextFieldDelegate 协议中定义的方法区分开来。

但如果你真的想扩展协议,那么使用:

@protocol MyTextFieldDelegate <UITextFieldDelegate>
- (void)textfieldDidSomething:(UITextField*)textField;
@end

【讨论】:

    【解决方案2】:

    在添加类别时,建议将其添加为单独的文件,命名约定为 MyTextFieldDelegate+MyTextFieldDelegateExtras.h

    【讨论】:

    • 也许显示一些代码而不是纯文本会很有用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多