iOS-传值大全
来源:互联网 发布:adams导入数据 编辑:程序博客网 时间:2024/06/05 12:00
前言
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页面:
先声明一个属性:
@interface SecondViewController : UIViewController@property (nonatomic,copy) NSString *passValue;@end
使用传的值UILabel *test = [[UILabel alloc]init]; test.textAlignment = NSTextAlignmentCenter; test.backgroundColor = [UIColor colorWithRed:0.961 green:0.961 blue:0.961 alpha:1.00]; test.frame = CGRectMake((ScreenWidth - 200)/2, (ScreenHeight - 40)/2, 200, 40); test.text = _passValue; [self.view addSubview:test];
2.文件传值
A页面:
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; [ud setObject:self.navigationItem.title forKey:@"passValue"]; [ud synchronize];
B页面:
NSUserDefaults *ud = [NSUserDefaults standardUserDefaults]; NSString *str = [ud objectForKey:@"passValue"]; NSLog(@"str == %@",str);
3.代理传值
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.广播传值
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:(void *)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);
希望能帮助到你!
0 0
- iOS-传值大全
- iOS-传值大全
- iOS-OC-iOS传值大全(代理传值,block 传值,单例传值,通知传值,属性传值)
- IOS命令大全
- iOS官方Sample大全
- iOS官方Sample大全
- iOS官方Sample大全
- iOS官方Sample大全
- IOS-路径大全
- IOS-路径大全
- iOS官方Sample大全
- IOS DEMO 大全
- iOS官方Sample大全
- IOS-路径大全
- iOS官方Sample大全
- iOS Socket使用大全
- IOS-读写文件大全
- IOS Socket使用大全
- 5-18 银行业务队列简单模拟 (25分)
- c++ 模板详解(1)
- Android N 多窗口分析之freeform流程分析一(界面功能介绍)
- Maven实战指南 06
- 齐肯多夫定理
- iOS-传值大全
- List双括号初始化
- python中的项目和应用的关系
- webcollector 初探(三)
- feof()和EOF的用法—— C中文件结尾的判断
- c++模板详解(2)
- [hihocoder1038]01背包问题
- bp神经网络及matlab实现
- Android中的加密方法