zoukankan      html  css  js  c++  java
  • IOS View传统的价值观之间

    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];要得到

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    问题.NET--win7 IIS唯一密钥属性“VALUE”设置为“DEFAULT.ASPX”时,无法添加类型为“add”的重复集合
    java传统web项目添加maven管理jar包,log4j无法正常输出日志
    TortoiseSVN设置Beyond Compare为版本比较、差异合并工具
    Win10以管理员身份启动cmd.exe
    SprinMVC中文件上传只在内存保留一份拷贝
    maven项目运行报错invalid LOC header (bad signature)
    c#编写windows服务在开机是OnStart启动超时
    centos安装mysql
    CENTOS7配置静态IP
    @__CheckForDebuggerJustMyCode@4
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4834293.html
Copyright © 2011-2022 走看看