iOS值传递
时间:
2017-03-24
分类:
iOS
标签:
值传递
/
代码段
/
属性传值
/
NSUserDefaults
/
delegate
/
NSNotificationCenter
/
KVO
/
blcok
前言
iOS传值方式很多,这里面我主要讲几种常用的传值;
以下传值正向传值A页面到B页面,反向传值B页面传到A页面。
1.属性传值
// A页面:
SecondViewController *svc = [[SecondViewController alloc]init];
UINavigationController *nvc = [[UINavigationController alloc]initWithRootViewController:svc];
svc.passValue = @"这是传的值";
[self presentViewController:nvc animated:YES completion:nil];
// B页面: 先声明一个属性:
@property (nonatomic,copy) NSString *passValue;
使用传的值:
NSLog(@"%@",_passValue);
2.NSUserDefaults传值
// A页面:
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
[ud setObject:@"这是传的值" forKey:@"passValue"];
[ud synchronize];
// B页面:
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
NSString *str = [ud objectForKey:@"passValue"];
NSLog(@"str == %@",str);
3.delegate传值
// B页面设置代理
@protocol ChangeValueDelegate <NSObject>
- (void)changeValueWithPassValue:(NSString *)passValue;
@end
@interface DetailViewController : UIViewController
@property (assign, nonatomic) id<ChangeValueDelegate> delegate;
@end
// 赋值
if ([self.delegate respondsToSelector:@selector(changeValueWithPassValue:)]) {
[self.delegate changeValueWithPassValue:@"这是传的值"];
}
/****************************************************/
// A页面遵循代理
@interface RootViewController ()<ChangeValueDelegate>
SecondViewController *dvc = [[SecondViewController alloc] init];
dvc.delegate = self;
[self.navigationController pushViewController:dvc animated:YES];
// 实现代理
- (void)changeValueWithPassValue:(NSString *)titleStr{
NSLog(@"titleStr == %@",titleStr);
}
4.NSNotificationCenter传值
// A页面发送通知
NSDictionary *dict = [NSDictionary dictionaryWithObject:@"我是传的值" forKey:@"value"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"KPassVaule" object:nil userInfo:dict];
// B页面接收通知
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getPassVauleget:) name:@"KPassVaule" object:nil];
- (void)getPassVauleget:(NSNotification *)notifi{
NSString *str = [notifi.userInfo objectForKey:@"value"];
NSLog(@"str == %@",str);
}
5.KVO传值
// 在初始化方法中加入
[_stu addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
// 在dealloc中移除KVO监听:
[_stu removeObserver:self forKeyPath:@"name" context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(voidvoid *)context{
if ([object isKindOfClass:[Student class]]) {
NSLog(@"_stu-old:%@",[change objectForKey:@"old"]);
NSLog(@"_stu-new:%@",[change objectForKey:@"new"]);
}
}
6.blcok反向传值
// A页面
DetailViewController *dvc = [[DetailViewController alloc] init];
dvc.fontSize = _label.font.pointSize;
void (^mBlocks)(float value);
mBlocks = ^(float value){
_label.font = [UIFont systemFontOfSize:value];
};
[dvc changeFont:mBlocks];
[self presentViewController:dvc animated:YES completion:^{
NSLog(@"hb");
}];
// B页面
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController{
void (^_mBlock)(float value);
}
@property (assign, nonatomic) float fontSize;
- (void)changeFont:(void (^)(float value))mBlock;
@end
// 传值
_mBlock(1111.00);