UI總結(jié)-界面?zhèn)髦?屬性傳值,協(xié)議傳值,block傳值,通知中指傳值)
在編程過程中,界面?zhèn)髦凳呛苤匾囊徊糠?常用的傳值方式就有四種:屬性傳值,協(xié)議傳值,block傳值,通知中指傳值,下面通過簡單的代碼來實(shí)現(xiàn)這四種傳值方式:
前一個(gè)界面ViewController.m文件:
#import "ViewController.h"
#import "SecondViewController.h"
//4.簽訂協(xié)議
@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];
//屬性傳值
//在要傳值的后一個(gè)頁面定義屬性,在對應(yīng)的視圖控制器調(diào)用屬性進(jìn)行賦值,就可以將值傳到后一個(gè)頁面
vc.str = self.text.text;
vc.arr = @[@"james", @"alice", @"tom"];
//5.設(shè)置代理人
vc.delegate = self;
//先寫一個(gè)block
void(^block)() = ^(){
self.view.backgroundColor = [UIColor blueColor];
};
//傳值
vc.block = block;
[vc release];
}
//6.實(shí)現(xiàn)代理方法
-(void)sendValue:(NSString *)str{
self.title = str;
}
-(void)jieshou:(NSNotification *)notification{
self.text.text = [notification.userInfo valueForKey:@"name"];
}
后一個(gè)界面SecondViewController.h文件:
#import
//1.聲明協(xié)議
@protocol SecondViewControllerDelegate
-(void)sendValue:(NSString *)str;
@end
@interface SecondViewController : UIViewController
@property(nonatomic, retain)NSString *str;
@property(nonatomic, retain)NSArray *arr;
//2.設(shè)置代理人屬性@property(nonatomic, assign)iddelegate;
//block傳值
@property(nonatomic, copy)void(^block)(void);
@end
后一個(gè)界面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.觸發(fā)協(xié)議生效
[self.delegate sendValue:self.text.text];
//調(diào)用block
self.block();
[[NSNotificationCenter defaultCenter]postNotificationName:@"text" object:nil userInfo:@{@"name":@"james"}];
}