UI总结-界面传值

UI总结-界面传值(属性传值,协议传值,block传值,通知中指传值)

在编程过程中,界面传值是很重要的一部分,常用的传值方式就有四种:属性传值,协议传值,block传值,通知中指传值,下面通过简单的代码来实现这四种传值方式:

前一个界面ViewController.m文件:

#import "ViewController.h"

#import "SecondViewController.h"

//4.签订协议

@interface ViewController ()

@property(nonatomic, retain)UITextField *text;

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

self.view.backgroundColor = [UIColor whiteColor];

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

btn.backgroundColor = [UIColor redColor];

[self.view addSubview:btn];

btn.frame = CGRectMake(100, 100, 200, 50);

btn.layer.borderWidth = 1;

btn.layer.cornerRadius = 5;

[btn setTitle:@"下一页" forState:UIControlStateNormal];

[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

self.text = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 50)];

self.text.backgroundColor = [UIColor blueColor];

[self.view addSubview:self.text];

[_text release];

self.text.layer.borderWidth = 1;

self.text.layer.cornerRadius = 5;

//第四种传值方式:通知中心

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(jieshou:) name:@"text" object:nil];

}

-(void)click:(UIButton *)button{

SecondViewController *vc = [[SecondViewController alloc]init];

[self.navigationController pushViewController:vc animated:YES];

//属性传值

//在要传值的后一个页面定义属性,在对应的视图控制器调用属性进行赋值,就可以将值传到后一个页面

vc.str = self.text.text;

vc.arr = @[@"james", @"alice", @"tom"];

//5.设置代理人

vc.delegate = self;

//先写一个block

void(^block)() = ^(){

self.view.backgroundColor = [UIColor blueColor];

};

//传值

vc.block = block;

[vc release];

}

//6.实现代理方法

-(void)sendValue:(NSString *)str{

self.title = str;

}

-(void)jieshou:(NSNotification *)notification{

self.text.text = [notification.userInfo valueForKey:@"name"];

}

后一个界面SecondViewController.h文件:

#import

//1.声明协议

@protocol SecondViewControllerDelegate

-(void)sendValue:(NSString *)str;

@end

@interface SecondViewController : UIViewController

@property(nonatomic, retain)NSString *str;

@property(nonatomic, retain)NSArray *arr;

//2.设置代理人属性@property(nonatomic, assign)iddelegate;

//block传值

@property(nonatomic, copy)void(^block)(void);

@end

后一个界面SecondViewController.m文件:

#import "SecondViewController.h"

@interface SecondViewController ()

@property(nonatomic, retain)UITextField *text;

@end

@implementation SecondViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.view.backgroundColor = [UIColor whiteColor];

NSLog(@"%@", self.str);

self.title = self.str;

NSLog(@"%@", self.arr);

UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];

btn.backgroundColor = [UIColor redColor];

[self.view addSubview:btn];

btn.frame = CGRectMake(100, 100, 200, 50);

btn.layer.borderWidth = 1;

btn.layer.cornerRadius = 5;

[btn setTitle:@"返回" forState:UIControlStateNormal];

[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

self.text = [[UITextField alloc]initWithFrame:CGRectMake(100, 200, 200, 50)];

self.text.backgroundColor = [UIColor blueColor];

[self.view addSubview:self.text];

[_text release];

self.text.layer.borderWidth = 1;

self.text.layer.cornerRadius = 5;

}

-(void)click:(UIButton *)button{

[self.navigationController popViewControllerAnimated:YES];

//3.触发协议生效

[self.delegate sendValue:self.text.text];

//调用block

self.block();

[[NSNotificationCenter defaultCenter]postNotificationName:@"text" object:nil userInfo:@{@"name":@"james"}];

}

运行结果如下:


UI总结-界面传值_第1张图片

你可能感兴趣的:(UI总结-界面传值)