美文网首页
iOS 代理使用

iOS 代理使用

作者: 清风_____ | 来源:发表于2020-05-23 10:40 被阅读0次

    1.代理类

    #import <UIKit/UIKit.h>
    
    NS_ASSUME_NONNULL_BEGIN
    
    @protocol InputViewDelegate <NSObject>
    
    -(void)btnClick:(UIButton*)sender textNumer:(NSString*)str1 textNumber:(NSString*)str2;
    
    @end
    
    @interface InputView : UIView
    
    @property (nonatomic,weak) id<InputViewDelegate> delegate;
    
    @end
    
    NS_ASSUME_NONNULL_END
    
    #import "InputView.h"
    
    @interface InputView()
    
    @property (weak, nonatomic) IBOutlet UITextField *OneTextField;
    @property (weak, nonatomic) IBOutlet UITextField *TwoTextField;
    
    @end
    
    @implementation InputView
    
    /*
    // Only override drawRect: if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    - (void)drawRect:(CGRect)rect {
        // Drawing code
    }
    */
    - (IBAction)btnClick:(UIButton *)sender {
        
        if ([self.delegate respondsToSelector:@selector(btnClick: textNumer: textNumber:)]) {
            // 调用代理对象的登录方法,代理对象去实现登录方法
                [self.delegate btnClick:sender textNumer:self.OneTextField.text textNumber:self.TwoTextField.text];
        }   
    }
    
    @end
    

    2.使用类

    #import "PlanCtl.h"
    @interface PlanCtl ()<InputViewDelegate>
    
    @property (nonatomic,strong) InputView *inputView;
    
    @end
    
    @implementation PlanCtl
    
    -(__kindof UIView *)inputView
    {
        if (_inputView == nil) {
            _inputView = [[[NSBundle mainBundle] loadNibNamed:@"InputView" owner:nil options:nil] lastObject];
            _inputView.delegate = self;
        }
        return _inputView;
    }
    // InputViewDelegate
    -(void)btnClick:(UIButton *)sender textNumer:(NSString *)str1 textNumber:(NSString *)str2
    {
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:iOS 代理使用

          本文链接:https://www.haomeiwen.com/subject/vadkahtx.html