iOS开发——代理与block传值
Posted 乱七八糟21号
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS开发——代理与block传值相关的知识,希望对你有一定的参考价值。
一、代理传值的方法
1.Hehe1ViewController.h中
#import <UIKit/UIKit.h>
@protocol Hehe1ViewControllerDelegate <NSObject>
- (void)backValueWith:(NSString*)str;
@end
@interface Hehe1ViewController : UIViewController
@property(nonatomic,weak) id delegate;
@end
2.Hehe1ViewController.m中
#import "Hehe1ViewController.h"
@interface Hehe1ViewController ()
@property(nonatomic,strong) UITextField *heheTextField;
@end
@implementation Hehe1ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITextField *heheTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 300, 40)];
self.heheTextField = heheTextField;
heheTextField.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:heheTextField];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
button1.frame = CGRectMake(20, 250, 300, 50);
button1.backgroundColor = [UIColor orangeColor];
[button1 setTitle:@"Back to VC" forState:UIControlStateNormal];
[button1 setTintColor:[UIColor whiteColor]];
[button1 addTarget:self action:@selector(goToHehe1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
}
- (void)goToHehe1 {
NSLog(@"hehe1...");
[_delegate backValueWith:self.heheTextField.text];
[self.navigationController popViewControllerAnimated:YES];
}
@end
二、block传值
1.Hehe2ViewController.h中
#import <UIKit/UIKit.h>
typedef void(^BackValue)(NSString *str);
@interface Hehe2ViewController : UIViewController
@property(nonatomic,copy) BackValue backValue;
- (void)returnStr:(BackValue)block;
@end
2.Hehe2ViewController.m中
#import "Hehe2ViewController.h"
@interface Hehe2ViewController ()
@property(nonatomic,strong) UITextField *heheTextField;
@end
@implementation Hehe2ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UITextField *heheTextField = [[UITextField alloc] initWithFrame:CGRectMake(20, 200, 300, 40)];
self.heheTextField = heheTextField;
heheTextField.backgroundColor = [UIColor lightGrayColor];
[self.view addSubview:heheTextField];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
button2.frame = CGRectMake(20, 250, 300, 50);
button2.backgroundColor = [UIColor orangeColor];
[button2 setTitle:@"Back to VC" forState:UIControlStateNormal];
[button2 setTintColor:[UIColor whiteColor]];
[button2 addTarget:self action:@selector(goToHehe2) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
}
- (void)returnStr:(BackValue)block {
self.backValue = block;
}
- (void)goToHehe2 {
NSLog(@"hehe2...");
self.backValue(self.heheTextField.text);
[self.navigationController popViewControllerAnimated:YES];
}
@end
三、在ViewController.m中接收传回的值
#import "ViewController.h"
#import "Hehe1ViewController.h" //delegate
#import "Hehe2ViewController.h" //block
#define klSceenWidth self.view.bounds.size.width
@interface ViewController ()
@property(nonatomic,strong) UILabel *label1;
@property(nonatomic,strong) UILabel *label2;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, klSceenWidth-40, 30)];
self.label1 = label1;
[self.view addSubview:label1];
//delegate
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
button1.frame = CGRectMake(20, 150, klSceenWidth-40, 30);
button1.backgroundColor = [UIColor orangeColor];
[button1 setTitle:@"go to hehe1" forState:UIControlStateNormal];
[button1 setTintColor:[UIColor whiteColor]];
[button1 addTarget:self action:@selector(goToHehe1) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
//block
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(20, 200, klSceenWidth-40, 30)];
self.label2 = label2;
[self.view addSubview:label2];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
button2.frame = CGRectMake(20, 250, klSceenWidth-40, 30);
button2.backgroundColor = [UIColor orangeColor];
[button2 setTitle:@"go to hehe1" forState:UIControlStateNormal];
[button2 setTintColor:[UIColor whiteColor]];
[button2 addTarget:self action:@selector(goToHehe2) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
}
- (void)backValueWith:(NSString*)str {
self.label1.text = str;
}
- (void)goToHehe1 {
Hehe1ViewController *heheVC1= [[Hehe1ViewController alloc] init];
heheVC1.delegate = self;
[self.navigationController pushViewController:heheVC1 animated:YES];
}
- (void)goToHehe2 {
Hehe2ViewController *heheVC2= [[Hehe2ViewController alloc] init];
[heheVC2 returnStr:^(NSString *str) {
self.label2.text = str;
}];
[self.navigationController pushViewController:heheVC2 animated:YES];
}
@end
以上是关于iOS开发——代理与block传值的主要内容,如果未能解决你的问题,请参考以下文章
iOS 页面间几种传值方式(属性,代理,block,单例,通知)
iOS 页面间几种传值方式(属性,代理,block,单例,通知)
iOS 页面间几种传值方式(属性,代理,block,单例,通知)