1、属性传值
前向后传值。
记住:
MainViewController与SecondViewController两个视图控制器,点击MainViewController中的按钮将跳转到SecondViewController视图,同时想要传递一个值过去。这时可以利用属性传值。
首先SecondViewController视图中需要有一个属性用来存储传递过来的值:
@property(nonatomic,retain) NSString*firstValue ;//属性传值
然后MainViewController视图需要引用SecondViewController视图的头文件,在视图中的按钮点击事件中,通过SecondViewController的对象将需要传递的值存在firstValue中:
//显示传过来的值[_txtFiled setText:_firstValue];//firstValue保存传过来的值
2、方法传值:
需求同一中的 属性传值一样,但是要通过使用方法传值,可以直接将方法与初始化方法合并,此时当触发MainViewController的按钮点击事件并跳转到SecondViewController时,在按钮点击事件中可以直接通过SecondViewController的初始化,将值保存在firstValue中:
初始化方法如下: 首先SecondViewController视图中需要有一个属性用来存储传递过来的值:
@property(nonatomic,retain) NSString *firstValue;//传值用
这样就可以直接通过firstValue属性获得传递过来的值:
//显示传过来的值[_txtFiledsetText:_firstValue];//firstValue保存传过来的值3:通知传值
通知中心NSNotificationCenter提供了一种更加解耦的方式。最典型的应用就是任何对象对可以发送通知到中心,同时任何对象可以监听中心的通知。发送通知的代码如下:
[[NSNotificationCenterdefaultCenter] postNotificationName:@”myNotificationName” object:broadcasterObject];注册接收通知的代码如下:
[[NSNotificationCenterdefaultCenter] addObserver:listenerObject selector:@selector(receivingMethodOnListener:) name:@”myNotificationName” object:nil];注册通知的时候可以指定一个具体的广播者对象,但这不是必须的。你可能注意到了defaultCenter。实际上这是你在应用中会使用到的唯一的中心。通知会向整个应用开放,因此只有一个中心。同时还有一个NSDistributedNotificationCenter。这是用来应用间通信的。在整个计算机上只有一个该类型的中心。优点: 通知的发送者和接受者都不需要知道对方。可以指定接收通知的具体方法。通知名可以是任何字符串。缺点: 较键值观察需要多点代码。在删掉前必须移除监听者。 不能传大量数值,只能让谁去做什么事。
4:代理传值(Delegate)
其中有两个ViewController分别对应两个界面,一个协议PassValueDelegate用来实现传值协议,UserEntity是传递数据的对象。以下是实现的效果:点击Open进入Second界面,输入完毕点击OK后回到First界面并显示结果
协议中声明的方法:
copy
import
@ class UserEntity;
@protocol PassValueDelegate
-( void)passValue:(UserEntity *)value;
@end
在第一个窗口实现协议:
import
import "PassValueDelegate.h"
//第一个窗口遵守PassValueDelegate
@interface ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UILabel *nameLabel;
@property (retain, nonatomic) IBOutlet UILabel *ageLabel;
@property (retain, nonatomic) IBOutlet UILabel *gendarLabel;
- (IBAction)openBtnClicked:(id)sender;
@end
.m文件中实现协议的方法:
[cpp]view plaincopy
//实现协议,在第一个窗口显示在第二个窗口输入的值方法
-( void)passValue:(UserEntity *)value
{
self.nameLabel.text = value.userName;
self.ageLabel.text = [NSString stringWithFormat:@"%d",value.age];
self.gendarLabel.text = value.gendar;
}
点击Open按钮所触发的事件:
[cpp]view plaincopy
//点击进入第二个窗口的方法
-
(IBAction)openBtnClicked:(id)sender {
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:[NSBundle mainBundle]];
//设置第二个窗口中的delegate为第一个窗口的self
secondView.delegate = self;[self.navigationController pushViewController:secondView animated:YES];
}
第二个窗口中声明一个NSObject对象,该对象遵守PassValueDelegate协议:
[cpp]view plaincopy
import
import "PassValueDelegate.h"
@interface SecondViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *nameTextField;
@property (retain, nonatomic) IBOutlet UITextField *ageTextFiled;
@property (retain, nonatomic) IBOutlet UITextField *gendarTextField;
//这里用assign而不用retain是为了防止引起循环引用。
@property(nonatomic,assign) NSObject *delegate;
- (IBAction)okBtnClicked:(id)sender;
- (IBAction)closeKeyboard:(id)sender;
@end
输入完毕后,点击OK按钮所触发的事件:
[cpp]view plaincopy
-
(IBAction)okBtnClicked:(id)sender {
UserEntity *userEntity = [[UserEntity alloc] init];
userEntity.userName = self.nameTextField.text;
userEntity.gendar = self.gendarTextField.text;
userEntity.age = [self.ageTextFiled.text intValue];//通过委托协议传值
[self.delegate passValue:userEntity];
//退回到第一个窗口
[self.navigationController popViewControllerAnimated:YES];[userEntity release];
}
以上就实现了使用Delegate在两个ViewController之间传值,这种场景一般应用在进入子界面输入信息,完后要把输入的信息回传给前一个界面的情况,比如修改用户个人信息,点击修改进入修改界面,修改完后到显示界面显示修改后的结果。
5:Block传值1.第一页中 声明一个 block,需要传入一个颜色 , 让当前的view 变色
// 声明一个 block, 需要传入一个颜色 ,让当前的 view 变色
void (^changeColor)( UIColor *color) =^( UIColor *color){
self . view .backgroundColor = color;
};
2 . 第一页中//block 传值 ---------将 block 给第二个页面
SecondViewController *secondVC = [[SecondViewController alloc ] init ];
//block 传值 --------- 将 block给第二个页面
secondVC. block = changeColor;
3.第二页中定义 -- 当 block变量作为一个类的属性 , 必须要使用copy 修饰
//block 传值 --------- 将 block给第二个页面
//block 传值 --- 当 block变量作为一个类的属性 , 必须要使用 copy修饰
@property ( nonatomic , copy) void (^block)( UIColor *color);
4.在第二页中给block传值
//block 传值 --------- 将传值给 block
NSArray *array = [NSArray arrayWithObjects :[ UIColor yellowColor ], [UIColor cyanColor], [ UIColor greenColor ], [UIColor brownColor], nil];
self . block([array objectAtIndex :rand () % 4]);
还有单例传值丶数据共享等传值方式,但是平时工作中很少用到,就不做解释.最常用的几种传值方式也就以上五种,各有优缺点吧,希望这篇文章能帮到大家把,感谢各位~