本章介绍三种逆向传值方式(Delegate、Block、单例)、一种正向传值方式(属性)
Delegate传值:
核心代码:
SecondViewController.h
#import <UIKit/UIKit.h>
//<1>声明协议
@protocol SecondVCDelegate <NSObject>
- (void)changeText:(NSString *)text;
@end
@interface SecondViewController : UIViewController
//<2>声明代理
@property (nonatomic, assign) id<SecondVCDelegate>delegate;
@end
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()<
UITextFieldDelegate>
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
[self createTextField];
}
- (void)createTextField {
UITextField * tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, self.view.bounds.size.width-100, 30)];
tf.placeholder = @"请输入文字";
tf.delegate = self;
[self.view addSubview:tf];
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
//<5>调用代理
if (self.delegate && [self.delegate respondsToSelector:@selector(changeText:)]) {
[self.delegate changeText:textField.text];
}
[self dismissViewControllerAnimated:YES completion:nil];
return YES;
}
@end
ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
#import "HTTools.h"
@interface ViewController ()<
UITextViewDelegate,
SecondVCDelegate>
@end
@implementation ViewController {
UITextView * _tv;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createTextView];
[self createButton];
}
- (void)createTextView {
_tv = [[UITextView alloc] initWithFrame:CGRectMake(100, 200, self.view.bounds.size.width-200, 200)];
_tv.backgroundColor = [UIColor cyanColor];
_tv.delegate = self;
[self.view addSubview:_tv];
}
- (void)createButton {
UIButton * button = [HTTools createButton:CGRectMake(100, 100, self.view.bounds.size.width-200, 100) bgColor:nil title:@"传值" titleColor:[UIColor blackColor] tag:101 action:@selector(buttonClick:) vc:self];
[self.view addSubview:button];
}
- (void)buttonClick:(UIButton *)button {
SecondViewController * secondVC = [[SecondViewController alloc] init];
//<3>设置代理
secondVC.delegate = self;
[self presentViewController:secondVC animated:YES completion:nil];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[_tv resignFirstResponder];
}
#pragma mark - UITextViewDelegate
//开始编辑状态
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
[UIView animateWithDuration:0.5 animations:^{
//相对现在的位置向上移动100
textView.transform = CGAffineTransformMakeTranslation(0, 50);
} completion:nil];
return YES;
}
//结束编辑状态
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
[UIView animateWithDuration:0.5 animations:^{
//返回原始位置
textView.transform = CGAffineTransformMakeTranslation(0, 0);
} completion:nil];
return YES;
}
//打字的时候就会调用
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSLog(@"%@",text);
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
}
return YES;
}
#pragma mark - SecondVCDelegate
//<4>实现自定义方法
- (void)changeText:(NSString *)text {
_tv.text = [NSString stringWithFormat:@"%@---%@",_tv.text,text];
}
@end
Block传值
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
//<1>声明block
@property (nonatomic, copy) void(^block)(NSString *);
@end
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()<
UITextFieldDelegate>
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
[self createTextField];
}
- (void)createTextField {
UITextField * tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 250, 30)];
tf.placeholder = @"请输入文字";
tf.delegate = self;
[self.view addSubview:tf];
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
//<3>调用block
_block(textField.text);
[self dismissViewControllerAnimated:YES completion:nil];
return YES;
}
@end
ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
#import "HTTools.h"
@interface ViewController ()<
UITextViewDelegate>
@end
@implementation ViewController {
UITextView * _tv;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createTextView];
[self createButton];
}
- (void)createTextView {
_tv = [[UITextView alloc] initWithFrame:CGRectMake(100, 200, self.view.bounds.size.width-200, 200)];
_tv.backgroundColor = [UIColor cyanColor];
_tv.delegate = self;
[self.view addSubview:_tv];
}
- (void)createButton {
UIButton * button = [HTTools createButton:CGRectMake(100, 100, self.view.bounds.size.width-200, 100) bgColor:nil title:@"传值" titleColor:[UIColor blackColor] tag:101 action:@selector(buttonClick:) vc:self];
[self.view addSubview:button];
}
- (void)buttonClick:(UIButton *)button {
SecondViewController * secondVC = [[SecondViewController alloc] init];
//<2>实现block
secondVC.block = ^(NSString * text){
_tv.text = text;
};
[self presentViewController:secondVC animated:YES completion:nil];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[_tv resignFirstResponder];
}
#pragma mark - UITextViewDelegate
//开始编辑状态
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
[UIView animateWithDuration:0.5 animations:^{
//相对现在的位置向上移动100
textView.transform = CGAffineTransformMakeTranslation(0, 50);
} completion:nil];
return YES;
}
//结束编辑状态
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
[UIView animateWithDuration:0.5 animations:^{
//返回原始位置
textView.transform = CGAffineTransformMakeTranslation(0, 0);
} completion:nil];
return YES;
}
//打字的时候就会调用
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSLog(@"%@",text);
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
}
return YES;
}
@end
单例传值
SecondViewController.m
#import "SecondViewController.h"
#import "HTTools.h"
@interface SecondViewController ()<
UITextFieldDelegate>
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
[self createTextField];
}
- (void)createTextField {
UITextField * tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 250, 30)];
tf.placeholder = @"请输入文字";
tf.delegate = self;
[self.view addSubview:tf];
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
//单例传值
[HTTools shareInstanced].text = textField.text;
[self dismissViewControllerAnimated:YES completion:nil];
return YES;
}
@end
ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
#import "HTTools.h"
@interface ViewController ()<
UITextViewDelegate>
@end
@implementation ViewController {
UITextView * _tv;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createTextView];
[self createButton];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if ([HTTools shareInstanced].text) {
_tv.text = [HTTools shareInstanced].text;
}
}
- (void)createTextView {
_tv = [[UITextView alloc] initWithFrame:CGRectMake(100, 200, self.view.bounds.size.width-200, 200)];
_tv.backgroundColor = [UIColor cyanColor];
_tv.delegate = self;
[self.view addSubview:_tv];
}
- (void)createButton {
UIButton * button = [HTTools createButton:CGRectMake(100, 100, self.view.bounds.size.width-200, 100) bgColor:nil title:@"传值" titleColor:[UIColor blackColor] tag:101 action:@selector(buttonClick:) vc:self];
[self.view addSubview:button];
}
- (void)buttonClick:(UIButton *)button {
SecondViewController * secondVC = [[SecondViewController alloc] init];
[self presentViewController:secondVC animated:YES completion:nil];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[_tv resignFirstResponder];
}
#pragma mark - UITextViewDelegate
//开始编辑状态
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
[UIView animateWithDuration:0.5 animations:^{
//相对现在的位置向上移动100
textView.transform = CGAffineTransformMakeTranslation(0, 50);
} completion:nil];
return YES;
}
//结束编辑状态
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
[UIView animateWithDuration:0.5 animations:^{
//返回原始位置
textView.transform = CGAffineTransformMakeTranslation(0, 0);
} completion:nil];
return YES;
}
//打字的时候就会调用
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSLog(@"%@",text);
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
}
return YES;
}
@end
属性传值
核心代码:
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@property (nonatomic, copy) NSString *text;
@end
SecondViewController.m
#import "SecondViewController.h"
@interface SecondViewController ()<
UITextFieldDelegate>
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
[self createTextField];
}
- (void)createTextField {
UITextField * tf = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 250, 30)];
tf.placeholder = @"请输入文字";
tf.text = self.text;
tf.delegate = self;
[self.view addSubview:tf];
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self dismissViewControllerAnimated:YES completion:nil];
return YES;
}
@end
ViewController.m
#import "ViewController.h"
#import "SecondViewController.h"
#import "HTTools.h"
@interface ViewController ()<
UITextViewDelegate>
@end
@implementation ViewController {
UITextView * _tv;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self createTextView];
[self createButton];
}
- (void)createTextView {
_tv = [[UITextView alloc] initWithFrame:CGRectMake(100, 200, self.view.bounds.size.width-200, 200)];
_tv.backgroundColor = [UIColor cyanColor];
_tv.delegate = self;
[self.view addSubview:_tv];
}
- (void)createButton {
UIButton * button = [HTTools createButton:CGRectMake(100, 100, self.view.bounds.size.width-200, 100) bgColor:nil title:@"传值" titleColor:[UIColor blackColor] tag:101 action:@selector(buttonClick:) vc:self];
[self.view addSubview:button];
}
- (void)buttonClick:(UIButton *)button {
SecondViewController * secondVC = [[SecondViewController alloc] init];
secondVC.text = _tv.text;
[self presentViewController:secondVC animated:YES completion:nil];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[_tv resignFirstResponder];
}
#pragma mark - UITextViewDelegate
//开始编辑状态
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView {
[UIView animateWithDuration:0.5 animations:^{
//相对现在的位置向上移动100
textView.transform = CGAffineTransformMakeTranslation(0, 50);
} completion:nil];
return YES;
}
//结束编辑状态
- (BOOL)textViewShouldEndEditing:(UITextView *)textView {
[UIView animateWithDuration:0.5 animations:^{
//返回原始位置
textView.transform = CGAffineTransformMakeTranslation(0, 0);
} completion:nil];
return YES;
}
//打字的时候就会调用
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
NSLog(@"%@",text);
if ([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
}
return YES;
}
@end