ios中view和view的传值(NSUserDefaults和通知中心)
来源:互联网 发布:淘宝小号怎么快速上心 编辑:程序博客网 时间:2024/06/18 06:37
原文地址: http://blog.csdn.net/gyz413977349/article/details/38333951
1、利用NSUserDefaults来传值,这种方法只限于传少量数据的情形:
比如你要传一个float的值,在需要传的时候用
[[NSUserDefaults standardUserDefaults] setFloat:float forKey::@”float”]
接收值的时候用
[[NSUserDefaults standardUserDefaults] floatForKey:@”float”]
2、NSNotificationCenter来传值
- (void)touchesEnded:(NSSet )touches withEvent:(UIEvent )event
{
UITouch * touch = [touches anyObject];CGPoint point = [touch locationInView:self];CGRect roundRect = [self rectNeedRefreshFromThisPoint:point];mLastPoint = CGPointMake(-1, -1);NSLog(@"%s: point(%f,%f)", __FUNCTION__, point.x, point.y);[self addToCurrentLineWithPoint:point.x y:point.y];[self endLine];[self setNeedsDisplayInRect:roundRect];NSNumber *pointX = [NSNumber numberWithFloat:point.x];NSNumber *pointY = [NSNumber numberWithFloat:point.y];NSDictionary *pointDict = [NSDictionary dictionaryWithObjectsAndKeys:pointX,@"pointX",pointY,@"pointY", nil];[[NSNotificationCenter defaultCenter]postNotificationName:@"passTouchedEndPointMsg" object:self userInfo:pointDict];
}
在消息中心的函数:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(getTouchedPoint:) name:@"passTouchedEndPointMsg" object:nil];
- (void) getTouchedPoint
:(NSNotification *)noti
{
NSDictionary *pointDict = [noti userInfo];touchedEndPointX = [[pointDict objectForKey:@"pointX"] floatValue];touchedEndPointY = [[pointDict objectForKey:@"pointY"] floatValue];NSLog(@"%f:%f",touchedEndPointX,touchedEndPointY);
}
用消息来传参数有下面几点说法:object指的是发送者、在poseter端的userInfo里面可以存放要传的参数,必须为NSDictionary类型。在center端获取这个dictionary类型用:[notification userInfo];来获取
0 0
- ios中view和view的传值(NSUserDefaults和通知中心)
- iOS几种常用的通信机制(代理,通知中心 ,NSUserDefaults)
- 【iOS】View跳转和传值
- ios中View.Bounds和View.Frame区别
- ios中view和controller设计原则的小结
- iOS中View和layer的区别以及layer用法
- IOS中通知中心(NSNotificationCenter)的使用
- IOS中通知中心(NSNotificationCenter)的使用
- iOS view和viewController的生命周期
- iOS view和viewController的生命周期
- iOS view和viewController的生命周期
- iOS view和viewController的生命周期
- iOS view和viewController的…
- iOS view和viewController的生命周期
- iOS view和viewController的生命周期
- iOS view层的组织和调用
- iOS view和viewController的生命周期
- iOS View frame和bounds的区别
- Java中WebService实例
- LaTeX中插入Visio图片
- ARM处理器Linux下浮点运算单元运用
- 聚集索引和非聚集索引(整理)
- 关于init初始化方法里调用self.view
- ios中view和view的传值(NSUserDefaults和通知中心)
- Hex文件解读
- Android 颜色,6位和8位数颜色的区别和应用
- transition
- No idea
- 博客搬家通知
- 怎样实现MindMapper中图例的运用
- Servlet 请求转发和重定向
- 工作队列