iOS传值问题
来源:互联网 发布:青海蒙古 知乎 编辑:程序博客网 时间:2024/05/14 16:16
传值:
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
{
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;
};
- iOS传值问题
- iOS 传值问题总结
- iOS-通知正向传值问题
- ios使用委托传值问题。
- iOS中的逆向传值问题
- IOS国际化取值问题
- 关于iOS 中得block传值问题献给新手
- IOS推出其他页面特殊传值 的问题
- 在iOS开发中关于正向传值的问题
- iOS 异步请求网络数据,传值问题
- iOS问题
- iOS问题
- iOS问题
- iOS问题
- IOS中关于色值的问题
- iOS 浮点型四舍五入精确值问题
- 问题:iOS 11 定位问题
- 工作记录8:iOS 传值问题总结(7种传值完美介绍)
- centos6.5 之电信宽带上网(拨号连接)
- IOS中NSUserDefaults的用法(轻量级本地数据存储)
- windows10中最强大王爽汇编语言学习环境
- [C/C++]函数参数的入栈顺序与可变参数的实现
- POJ1141 Brackets Sequence (最小括号匹配升级版:区间DP+打印路径)
- iOS传值问题
- 为什么析构函数可以为virtual型,而构造函数则不能?
- 黑苹果,蓝牙折腾经历
- Windows 7下JDK与Ant的安装和环境变量配置
- 修改配置信息,并保存到文件
- Mahout学习之命令行创建序列文件
- AngularJS 过滤器
- Eclipse 中常用的快捷键
- android中的空格编码 string.xml前后加空格的技巧