Object-C Target-Action模式 回调

target - action设计模式 是一种比较常见的设计模式,比如我们在用button的时候,给button添加一个事件,我们就用到了target - action设计模式.现在我们就用target - action设计模式 做一个button,还原button的设计原理,当然了 我们还要加上tap手势.

human.h

//
//  Human.h
//  ZWLimitCounterDemo
//
//  Created by nan on 2021/12/9.
//  Copyright © 2021 wang_ziwu. All rights reserved.
//

#ifndef Human_h
#define Human_h

#import <Foundation/Foundation.h>
  
@interface Human : NSObject
-(void)say:(id)target Action:(SEL)action;
@end

#endif /* Human_h */

Human.m

//
//  Human.m
//  ZWLimitCounterDemo
//
//  Created by nan on 2021/12/9.
//  Copyright © 2021 wang_ziwu. All rights reserved.
//

#import "Human.h"
  
@implementation Human
-(void)say:(id)target Action:(SEL)action
{
   // IMP imp1 = [target methodForSelector:action];
    [target performSelector:action withObject:@"Human中的say方法"];
    NSLog(@"Human中的say方法");
}
@end

ViewController.h

//
//  ViewController.h
//  ZWLimitCounterDemo
//
//

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

ViewController.m

//
//  ViewController.m
//  ZWLimitCounterDemo
//
//
#import "ManagerMessage.h"
#import "ComponentBlock.h"
#import "ObjcObject.h"
#import "Human.h"
#import "WKLLogger.h"

#import "ViewController.h"
#import "UITextView+ZWLimitCounter.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *firstTextView;
- (IBAction)imppress:(id)sender;

@end

@implementation ViewController {  
}

//回调函数
-(void)messageCallBack:(NSString *)string
{
    self.firstTextView.text=string;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self configFirstTextView];
    [self configSecndTextView];
    self.firstTextView.text=@"测试";

}
- (void)configFirstTextView{
    self.firstTextView.layer.borderWidth = 1;
    self.firstTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    self.firstTextView.zw_limitCount = 10;
}
- (void)configSecndTextView{
    CGRect rect = CGRectMake(10, 200, [UIScreen mainScreen].bounds.size.width-20, 60);
    UITextView *secondTextView = [[UITextView alloc] initWithFrame:rect];
    secondTextView.layer.borderWidth = 1;
    secondTextView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    secondTextView.zw_limitCount = 10;
    [self.view addSubview:secondTextView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (IBAction)imppress:(id)sender {
    //Class c =NSClassFromString(@"Human");
    //[[c new] say];
    
    Human *human =[Human new];
    [human say:self Action:@selector(messageCallBack:)];
     
}


@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值