zoukankan      html  css  js  c++  java
  • 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];来获取

  • 相关阅读:
    IDEA 错误:程序包XXX不存在
    202A 202B 202C 202D 202E字符的作用及解释
    MySQL 获取每月多少日的sql写法
    Mybatis Plus使用租户过滤无效解决方案
    Shiro集成多个Realm,认证都不通过返回y configured realms. Please ensure that at least one realm can authenticate these tokens.
    使用IDEA开发SpringBoot不加载application.yml配置文件的解决方案
    集成SpringCloudBus,但没有总线通知更改
    Gradle 使用@Value注册编译报错
    Shiro Session放到Redis中常遇到的问题
    前端页面调试方式的选择
  • 原文地址:https://www.cnblogs.com/wengzilin/p/2400404.html
Copyright © 2011-2022 走看看