iOS传值方法
2016-03-01 15:52
691 查看
传值:
1>单例传值:
(1)发送方:((AppDelegate *)[UIApplication sharedApplication].delegate).color=[UIColor orangeColor];
接收方: UIColor * color=((AppDelegate *)[UIApplication sharedApplication].delegate).color;
self.view.backgroundColor=color;
//必须在AppDelegate声明一个属性@property (retain) UIColor * color;
(2)先建一个类继承NSObject实现一个+方法并且要声明它传值的属性
DataCenter.h:
+(DataCenter *)sharedDC;
@property (copy) NSString * userName;
@property (copy) NSString * password;
DataCenter.m:
+(DataCenter *)sharedDC
{
4000
static DataCenter * theDataCenter=nil;
if (theDataCenter==nil) {
theDataCenter=[[DataCenter alloc]init];
}
return theDataCenter;
}
发送方:DataCenter * theDC=[DataCenter sharedDC];
theDC.userName=_userNameTF.text;
theDC.password=_passwordTF.text;
接收方:
NSString * userName=[DataCenter sharedDC].userName;
NSString * password=[DataCenter sharedDC].password;
_userInfoLab.text=[NSString stringWithFormat:@”用户名:%@\n用户密码:%@”,userName,password];
2>代理传值:
先遵守一个ChangeColor协议:-(void)itIsTimeToChangeColor:(UIColor *)color;
发送方:if ([self.delegate respondsToSelector:@selector(itIsTimeToChangeColor:)]) {
[self.delegate itIsTimeToChangeColor:color];
}
但必须设置个属性:@property (assign) id delegate;
接收方:-(void)itIsTimeToChangeColor:(UIColor *)color{
self.view.backgroundColor=color;
}但必须遵守协议
3>广播传值:
发送方: //发送一个广播,广播携带参数
[[NSNotificationCenter defaultCenter]postNotificationName:@”changColor” object:oneColor];
接收方: [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(needChangeColor:) name:@”changColor” object:nil];
-(void)needChangeColor:(NSNotification *)notification{
UIColor * color=notification.object;
self.view.backgroundColor=color;
}
4>block传值:
先在发送方声明block属性:有参返回值与无参返回值(下面是带参有参返回值)
typedef UIColor (^block)(UIColor );
发送方:
UIColor * redColor=[UIColor redColor];
UIColor * color1= self.Handle(redColor);
self.view.backgroundColor=color1
接收方:
secondViewController * secondVC=[[secondViewController alloc]init];
secondVC.Handle=^(UIColor * color){
self.view.backgroundColor=color;
return _greenColor;
};
1>单例传值:
(1)发送方:((AppDelegate *)[UIApplication sharedApplication].delegate).color=[UIColor orangeColor];
接收方: UIColor * color=((AppDelegate *)[UIApplication sharedApplication].delegate).color;
self.view.backgroundColor=color;
//必须在AppDelegate声明一个属性@property (retain) UIColor * color;
(2)先建一个类继承NSObject实现一个+方法并且要声明它传值的属性
DataCenter.h:
+(DataCenter *)sharedDC;
@property (copy) NSString * userName;
@property (copy) NSString * password;
DataCenter.m:
+(DataCenter *)sharedDC
{
4000
static DataCenter * theDataCenter=nil;
if (theDataCenter==nil) {
theDataCenter=[[DataCenter alloc]init];
}
return theDataCenter;
}
发送方:DataCenter * theDC=[DataCenter sharedDC];
theDC.userName=_userNameTF.text;
theDC.password=_passwordTF.text;
接收方:
NSString * userName=[DataCenter sharedDC].userName;
NSString * password=[DataCenter sharedDC].password;
_userInfoLab.text=[NSString stringWithFormat:@”用户名:%@\n用户密码:%@”,userName,password];
2>代理传值:
先遵守一个ChangeColor协议:-(void)itIsTimeToChangeColor:(UIColor *)color;
发送方:if ([self.delegate respondsToSelector:@selector(itIsTimeToChangeColor:)]) {
[self.delegate itIsTimeToChangeColor:color];
}
但必须设置个属性:@property (assign) id delegate;
接收方:-(void)itIsTimeToChangeColor:(UIColor *)color{
self.view.backgroundColor=color;
}但必须遵守协议
3>广播传值:
发送方: //发送一个广播,广播携带参数
[[NSNotificationCenter defaultCenter]postNotificationName:@”changColor” object:oneColor];
接收方: [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(needChangeColor:) name:@”changColor” object:nil];
-(void)needChangeColor:(NSNotification *)notification{
UIColor * color=notification.object;
self.view.backgroundColor=color;
}
4>block传值:
先在发送方声明block属性:有参返回值与无参返回值(下面是带参有参返回值)
typedef UIColor (^block)(UIColor );
发送方:
UIColor * redColor=[UIColor redColor];
UIColor * color1= self.Handle(redColor);
self.view.backgroundColor=color1
接收方:
secondViewController * secondVC=[[secondViewController alloc]init];
secondVC.Handle=^(UIColor * color){
self.view.backgroundColor=color;
return _greenColor;
};
相关文章推荐
- Ruby中require、load、include、extend的区别介绍
- Ruby中的p和puts的使用区别浅析
- Ruby中的block、proc、lambda区别总结
- Redis和Memcached的区别详解
- Lua中调用函数使用点号和冒号的区别
- Lua中关于求模与求余的区别介绍
- TMP、TEMP和TMP文件区别解析
- c#窗体传值用法实例详解
- C#基础语法:结构和类区别详解
- 深入c# 类和结构的区别总结详解
- C#简单实现子窗体向父窗体传值的方法
- C#中string.Empty和null的区别详解
- sqlserver和oracle中对datetime进行条件查询的一点区别小结
- 网页中Span和Div的区别
- 大家看了就明白了css样式中类class与标识id选择符的区别小结
- C#中类与结构的区别实例分析
- SQL 中having 和where的区别分析
- c#中(int)、int.Parse()、int.TryParse、Convert.ToInt32的区别详解
- MySQL Antelope和Barracuda的区别分析
- Rails Routes中new、collection、member的区别浅析