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