Protocol - 5

来源:互联网 发布:c语言开平方怎么写 编辑:程序博客网 时间:2024/04/29 10:39

在放一个关于委托协议传值的规范写法:

协议的定义:

#import <UIKit/UIKit.h>@class KeyBordView;@protocol KeyBordVIewDelegate <NSObject>- (void)beginRecord;- (void)finishRecord;- (void)KeyBordView:(KeyBordView* )keyBoardView textFiledBegin:(UITextField *)textFiled;- (void)KeyBordView:(KeyBordView* )keyBoardView textFiledReturn:(UITextField *)textFiled;@end

定义“委托者” delegate:

@interface KeyBordView : UIView@property (nonatomic, assign) id <KeyBordVIewDelegate> delegate;@end

在KeyBordView实现文件中的对委托方法的传值,这里传的是self,self.textFiled

注意到这里非常规范的先判断委托是否被赋予了(被委托者),具体是用respondsToSelector来判断,非常规范

#pragma mark - UITextField代理方法- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{    if ([self.delegate respondsToSelector:@selector(KeyBordView:textFiledBegin:)])    {        [self.delegate KeyBordView:self textFiledBegin:textField];    }        return YES;}- (BOOL)textFieldShouldReturn:(UITextField *)textField{    if ([self.delegate respondsToSelector:@selector(KeyBordView:textFiledReturn:)])    {        [self.delegate KeyBordView:self textFiledReturn:textField];    }             return YES;}

最后是被委托者实现委托方法!




同理:




0 0
原创粉丝点击