view间传值的方法总结
来源:互联网 发布:淘宝投诉没授权 编辑:程序博客网 时间:2024/05/06 08:57
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];来获取
- view间传值的方法总结
- view的getChildMeasureSpace方法总结
- Android中移动view的方法总结
- 实现View滑动的方法总结
- Android-View 移动的相关方法总结
- Android View滑动的方法总结
- 自定义View方法总结
- android View的三个构造方法 简单总结
- 自定义View的总结
- View的dispatchTouchEvent总结
- View的onMeasure方法
- view的requestLayout()方法
- view的onInterceptTouchEvent 方法
- View 的onMeasure方法
- view的requestLayout()方法
- view的requestLayout()方法
- View的各个方法
- View的drawableStateChanged()方法
- git笔记
- 正则表达式的应用
- 继电器的驱动电路——两种电路引发的争论及思考
- 人民币升值和贬值的影响
- 脏数据
- view间传值的方法总结
- 想你,但不爱你
- MYSQL的分区字段,必须包含在主键字段内 (重要)
- 补码表示法和负数的移位
- 文档的比较
- linux下dup和dup2
- 最近闲来无事做了个php网站源码下载的网站 求指导。。。
- 七夕情书之恋恋红尘
- 代码大全学习-31-系统集成(Integration)