下面我们讲讲几种比较常见的界面传值:正向传值(属性传值),block传值,代理传值,单例传值,通知传值。
)
首先 创建一个ViewController ,在创建一个跳转的控制器 FirstViewController
一、正向传值
1、首先在 FirstViewController.h 中 声明一个属性 (可以是NSString,NSArray,NSDictionary)
#import <UIKit/UIKit.h>
@interface FirstViewController : UIViewController
@property (nonatomic, copy) NSString * stringData;
@property (nonatomic, strong) NSArray * array;
@property (nonatomic, strong) NSDictionary * dict;
@end
2、在 FirstViewController.m 打印传的值
NSLog(@"正向传值------%@",self.stringData);
3、在ViewController.m 中
UIButton * first = [UIButton buttonWithType:UIButtonTypeCustom];
first.frame = CGRectMake([UIScreen mainScreen].bounds.size.width/2 - 100, 120, 200, 50);
[first setTitle:@"正向传值" forState:UIControlStateNormal];
[first addTarget:self action:@selector(toViewController:) forControlEvents:UIControlEventTouchUpInside];
first.titleLabel.font = [UIFont systemFontOfSize:15];
first.backgroundColor = [UIColor redColor];
[self.view addSubview:first];
- (void)toViewController:(UIButton *)sender {
FirstViewController * first = [[FirstViewController alloc] init];
first.stringData = @"正向传的值";
[self.navigationController pushViewController:first animated:YES];
}
运行
二、block 传值
1、在FirstViewController.h 中
@property (nonatomic, copy) void(^ myBlock) (NSString *);
2、在FirstViewController.m 中 在按钮的点击事件中,直接调用
self.myBlock(@“block传的值”);
3、在ViewController.m 中定义一个 UILable * changeLable;用来接受block传的值
changeLable = [[UILabel alloc] initWithFrame:CGRectMake(0, 380, [UIScreen mainScreen].bounds.size.width, 60)];
changeLable.textAlignment = NSTextAlignmentCenter;
[self.view addSubview:changeLable];
FirstViewController * first = [[FirstViewController alloc] init];
first.stringData = @"正向传的值";
first.myBlock = ^(NSString * string) {
changeLable.text = string;
};
[self.navigationController pushViewController:first animated:YES];
运行结果
三、代理
1、在FirstController.h 定义代理
@protocol PushToContentDelegate <NSObject>
- (void)pushToConttent:(id)content;
@end
@property (nonatomic, assign) id<PushToContentDelegate> delegate;
2、在FirstController.m中
// 通过代理传值首次判断代理是否存在,并在代理能够响应代理方法时才执行代理方法
if (self.delegate && [self.delegate respondsToSelector:@selector(pushToConttent:)]) {
[self.delegate pushToConttent:@"代理传的值"];
}
3、在ViewController.m中 , 实现该代理,并实现该代理的方法
@interface ViewController () <PushToContentDelegate>
{
UILabel * changeLable;
}
@end
- (void)toViewController:(UIButton *)sender {
FirstViewController * first = [[FirstViewController alloc] init];
first.stringData = @"正向传的值";
first.delegate = self;
[self.navigationController pushViewController:first animated:YES];
}
- (void)pushToConttent:(id)content {
changeLable.text = [NSString stringWithFormat:@"%@",content];
}
运行效果
四、通知
1、在ViewController.m 中注册一个通知监听,并在页面消失时移除该通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificeM:) name:@"PUSHTOCONTENT" object:nil];
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)notificeM : (NSNotification *)nof {
NSDictionary * dict = nof.userInfo;
changeLable.text = [NSString stringWithFormat:@"%@",dict[@"content"]];
}
2、在FirstViewController.m 中 发送通知
[[NSNotificationCenter defaultCenter] postNotificationName:@“PUSHTOCONTENT” object:self userInfo:@{@“content”?“通知传值”}];
运行结果
总结
- 属性传值就是在一个页面给另一个页面的属性赋值,通过赋值的这个属性来传递的信息;
- block传值和属性传值差不多,block代码块也是另一个界面的属性,block将值保存在代码块中,通过页面的回调代码块,以获取代码块传递过来的值。
- 代理方法是用于任意界面之间传值,只需要声明实现代理方法,就可以获取传递过来的值其实
- 注册通知与移除通知需要一一对应,同时通知名称要相同,才能收到该通知发送的消息。