iosios 属性默认值传值怎么回传

ios 正向传值反向传值_百度知道
ios 正向传值反向传值
ios 有几种传值方式 哪种可以正向传值 哪种穿盯扁故壮嘎憋霜铂睛可以反向传值 各自都怎么使用
有什么不同
哪几种传值方式可以通用
他们都什么时候使用
正向传值:一、利用present 的 block 块进行正向传值
RootViewController.m中:SubViewController * svc = [[SubViewControlleralloc]init];
//svc.label.text = @&正向Block传值&;
//这句是正向的属性传值,作用是让B页面,能持有A页面的地址,因B要向A进行反向传值
//想给谁传值,就要持有谁的地址
svc.delegate =[selfpresentViewController:svc animated:YEScompletion:^{
svc.label.text =@&正向Block传值&;
}];二、利用AB页面中的属性传值反向传值:一、利用block来进行 b界面向 a 界面反向传值a 中声明一个 block属性,将所要传的值放入block
函数指针中,b去接收.h文件@property(nonatomic,copy)void(^blockColor) (UIColor *);.m文件self.blockColor([UIColor blackColor]);注意用 copyb.m文件中 接收svc.blockColor = ^(UIColor *color){
self.view.backgroundColor =};二、通知中心和观察者模式反向传值例: viewController1向 viewController2传值1.Appdelegate 文件中,有一个接收所要传递的值的变量,用来保存所要传递的数据,界面1写入,界面2读取@property (nonatomic,copy)NSString *2.viewController1中,当要传值的时候(按钮按下时),将数据通过UIApplication单例获取到的Appdelegate *delegate,传入Appdelegate文件中。同时向通知中心发送一个广播[[NSNotificatio穿盯扁故壮嘎憋霜铂睛nCenter defaultCenter] postNotificationName:@&鬼子来了& object:nil];3.viewController2 中,向通知中心注册,添加自己,成为观察者。当数据改变时,用单例delegate 读取数据-(void)becomObserver{
//获得通知中心,注册成观察者
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeText) name:@&鬼子来了& object:nil];}//收到广播消息后调用的方法-(void)changeText{
//首先获取appDelegate的对象
AppDelegate * appDeleagte = [UIApplication sharedApplication].
NSString * string = appDeleagte.
NSLog(@&sss:%@&,string);
label.text = appDeleagte.
NSLog(@&label.txt = %@&,label.text);
}三、利用协议,界面反向传值 AppDelegateviewController2为委托方,viewController1为代理,在viewController2中委托方制定协议,并且属性delegate = viewController1,要遵循协议;在viewController1中,将viewController2对象的属性delegate设为自己(vc1)1.ViewController1.m
vc2.delegate =2.ViewController2.h文件//委托方制定协议,代理人必须遵守@protocol BackValueDelegate&NSObject&-(void)changeText:(NSString *)string@end@interface ViewController1 :UIViewController//用来设置代理,并且遵守协议@property (nonatomic,retain) id&BackValueDelegate&@end3. ViewController2.m文件 //利用代理去回传数据
[self.delegate changeText:@&****&];四、用系统自带的UIApplication单例进行传值五、用自己实现的单例进行传值六、用TargetAction 的方式进行两个界面来回反复调用传值
来自团队:
其他类似问题
为您推荐:
ios的相关知识
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁汇总ios开发逆向传值的方法
作者:xywjun
字体:[ ] 类型:转载 时间:
这篇文章主要为大家汇总了ios开发逆向传值的方法,感兴趣的小伙伴们可以参考一下
iOS的逆向传值有很多种方法,下面来总结几种常用的传值方式(只贴相关代码):
第一种:代理传值
第二个控制器:
@protocol WJSecondViewControllerDelegate &NSObject&
- (void)changeText:(NSString*)
@property(nonatomic,assign)id&WJSecondViewControllerDelegate&
- (IBAction)buttonClick:(UIButton*)sender {
_str = sender.titleLabel.
[self.delegate changeText:sender.titleLabel.text];
[self.navigationController popViewControllerAnimated:YES];
第一个控制器:
- (IBAction)pushToSecond:(id)sender {
WJSecondViewController *svc = [[WJSecondViewController alloc]initWithNibName:@"WJSecondViewController" bundle:nil];
svc.delegate =
svc.str = self.navigationItem.
[self.navigationController pushViewController:svc animated:YES];
[svc release];
- (void)changeText:(NSString *)text{
self.navigationItem.title =
第二种:通知传值
第一个控制器:
//注册监听通知
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(limitDataForModel:) name:@"NOV" object:nil];
- (void)limitDataForModel:(NSNotification *)noti{
self.gamesInfoArray = noti.
第二个控制器:
//发送通知
[[NSNotificationCenter defaultCenter]
postNotificationName:@"NOV" object:gameArray];
第三种:单例传值
Single是一个单例类,并且有一个字符串类型的属性titleName
在第二个控制器:
- (IBAction)buttonClick:(UIButton*)sender {
Single *single = [Single sharedSingle];
single.titleName = sender.titleLabel.
[self.navigationController popViewControllerAnimated:YES];
第一个控制器:
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
Single *single = [Single sharedSingle];
self.navigationItem.title = single.titleN
第四种:block传值
第二个控制器:
@property (nonatomic,copy) void (^changeText_block)(NSString*);
- (IBAction)buttonClick:(UIButton*)sender {
_str = sender.titleLabel.
self.changeText_block(sender.titleLabel.text);
[self.navigationController popViewControllerAnimated:YES];
第一个控制器:
- (IBAction)pushToSecond:(id)sender {
WJSecondViewController *svc = [[WJSecondViewController alloc]initWithNibName:@"WJSecondViewController" bundle:nil];
svc.str = self.navigationItem.
[svc setChangeText_block:^(NSString *str) {
&self.navigationItem.title =
[self.navigationController pushViewController:svc animated:YES];
第五种:extern传值
第二个控制器:
extern NSString *
- (IBAction)buttonClick:(UIButton*)sender {
btn = sender.titleLabel.
[self.navigationController popViewControllerAnimated:YES];
第一个控制器:
NSString *btn =
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationItem.title =
第六种:KVO传值
第一个控制器:
- (void)viewDidLoad {
[super viewDidLoad];
_vc =[[SecondViewController alloc]init];
//self监听vc里的textValue属性
[_vc addObserver:self forKeyPath:@"textValue" options:0 context:nil];
第二个控制器:
- (IBAction)buttonClicked:(id)sender {
self.textValue = self.textField.
[self.navigationController popViewControllerAnimated:YES];
其实还有很多种传值方式,比如说NSUserDefaults,先把数据保持在本地,再读取,或者写入plist及其它类型的文件再读取等等许多方式,在这里就不一一列举了!这些代码写的时间比较久了,今天整理了一下,还比较乱,有什么不对或不足的地方请见谅!
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具IOS随身笔记—属性传值
一.属性传值
MainViewController与SecondViewController两个视图,点击MainViewController中的将跳转到SecondViewController视图,同时想要传递一个值过去。这时可以利用属性传值。
首先SecondViewController视图中需要有一个属性用来传递过来的值:
@property(nonatomic,retain) NSString
*firstV//属性传值
然后MainViewController视图需要引用SecondViewController视图的头文件,在视图中的按钮点击事件中,通过SecondViewController的对象将需要传递的值存在firstValue中:
(void)buttonAction:(UIButton *)button{
SecondViewController *second = [[SecondViewController
alloc]init];
//用下一个视图的属性接受想要传过去的值,属性传值
second.firstValue = _txtFiled.
[self.navigationController pushViewController:second
animated:YES];
[second release];}
页面跳转之后,就能在SecondViewController视图中,通过存值的属性,取用刚才传递过来的值:
//显示传过来的值[_txtFiled
setText:_firstValue];//firstValue保存传过来的值
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。}

我要回帖

更多关于 ios 属性赋值 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信