博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
控制器属性传值的一些小问题
阅读量:6716 次
发布时间:2019-06-25

本文共 4535 字,大约阅读时间需要 15 分钟。

控制器属性传值的一些小问题

这篇博文并没有什么技术含量,属于很基础的知识,但也容易在感官上产生错误的认识,今天记录于此。

一个对象,无论是被转存到数组中,还是被多个控制器持有,如果这个对象没有发生值的拷贝(创建出新的对象),所有的对这个对象的持有者只持有了一个样本,修改了这个样本的值会影响到所有持有者。

效果演示图:

测试用源码:

DataModel.h 与 DataModel.m

////  DataModel.h//  BaseViewController////  Created by YouXianMing on 15/1/2.//  Copyright (c) 2015年 YouXianMing. All rights reserved.//#import 
@interface DataModel : NSObject@property (nonatomic, strong) NSString *name;@property (nonatomic, strong) NSNumber *age;@property (nonatomic, strong) NSArray *array;@end
////  DataModel.m//  BaseViewController////  Created by YouXianMing on 15/1/2.//  Copyright (c) 2015年 YouXianMing. All rights reserved.//#import "DataModel.h"@implementation DataModel@end
ChangeValueViewController.h 与 
ChangeValueViewController.m
////  ChangeValueViewController.h//  BaseViewController////  Created by YouXianMing on 15/1/2.//  Copyright (c) 2015年 YouXianMing. All rights reserved.//#import 
#import "DataModel.h"@interface ChangeValueViewController : UIViewController@property (nonatomic, strong) DataModel *dataModel;@end
////  ChangeValueViewController.m//  BaseViewController////  Created by YouXianMing on 15/1/2.//  Copyright (c) 2015年 YouXianMing. All rights reserved.//#import "ChangeValueViewController.h"@interface ChangeValueViewController ()@property (nonatomic, strong) UILabel *label;@end@implementation ChangeValueViewController- (void)viewDidLoad {    [super viewDidLoad];     self.view.backgroundColor = [UIColor yellowColor];    self.dataModel.age        = @(self.dataModel.age.intValue + 1);    self.dataModel.array      = @[@"2", @"1", @"2"];        [self createButton];    [self createLabel];}#pragma mark - 创建出label- (void)createLabel {    self.label               = [[UILabel alloc] initWithFrame:self.view.bounds];    self.label.text          = @"Back";    self.label.font          = [UIFont fontWithName:@"HelveticaNeue-Thin" size:70.f];    self.label.textAlignment = NSTextAlignmentCenter;    self.label.textColor     = [UIColor grayColor];    [self.view addSubview:self.label];}#pragma mark - 创建按钮以及按钮事件- (void)createButton {    UIButton *button = [[UIButton alloc] initWithFrame:self.view.bounds];    [self.view addSubview:button];    [button addTarget:self               action:@selector(buttonEvent:)     forControlEvents:UIControlEventTouchUpInside];}- (void)buttonEvent:(UIButton *)button {    [self dismissViewControllerAnimated:YES completion:^{            }];}@end
ViewController.m
////  ViewController.m//  BaseViewController////  Created by YouXianMing on 15/1/2.//  Copyright (c) 2015年 YouXianMing. All rights reserved.//#import "ViewController.h"#import "ChangeValueViewController.h"#import "DataModel.h"@interface ViewController ()@property (nonatomic, strong) DataModel *dataModel; // 数据model@property (nonatomic, strong) UILabel   *label;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // 数据model    self.dataModel       = [DataModel new];    self.dataModel.name  = @"YouXianMing";    self.dataModel.age   = @(27);    self.dataModel.array = @[@"1"];        [self createButton];    [self createLabel];}#pragma mark - 创建出label- (void)createLabel {    self.label               = [[UILabel alloc] initWithFrame:self.view.bounds];    self.label.text          = [NSString stringWithFormat:@"%@ - %@", self.dataModel.name, self.dataModel.age];    self.label.font          = [UIFont fontWithName:@"HelveticaNeue-Thin" size:30.f];    self.label.textAlignment = NSTextAlignmentCenter;    self.label.textColor     = [UIColor grayColor];    [self.view addSubview:self.label];}#pragma mark - 创建按钮以及按钮事件- (void)createButton {    UIButton *button = [[UIButton alloc] initWithFrame:self.view.bounds];    [self.view addSubview:button];    [button addTarget:self               action:@selector(buttonEvent:)     forControlEvents:UIControlEventTouchUpInside];}- (void)buttonEvent:(UIButton *)button {    ChangeValueViewController *changeValueViewCV = [ChangeValueViewController new];        // 获取数据    changeValueViewCV.dataModel                  = self.dataModel;        [self presentViewController:changeValueViewCV                       animated:YES                     completion:^{                                              }];}#pragma mark - 控制器view- (void)viewWillAppear:(BOOL)animated {    self.label.text = [NSString stringWithFormat:@"%@-%@", self.dataModel.name, self.dataModel.age];    NSLog(@"%@", self.dataModel.array);}@end

只要DataModel被ViewController一直持有,所有的持有这个DataModel的控制器,修改了DataModel的值,就会影响到全局的值,这点需要注意哦。

转载地址:http://jarlo.baihongyu.com/

你可能感兴趣的文章
6425C-Lab12 管理DC(1)
查看>>
RocketMQ调研笔记
查看>>
maven 注册 jar
查看>>
高并发写入mysql的设计
查看>>
成长点滴:我不知道该说些什么?
查看>>
linux之使用man查看命令手册
查看>>
IT管理员如何保证你的内网安全?
查看>>
用U盘安装debian系统
查看>>
Mac 下得Jmeter 测试
查看>>
java基础之本地线程
查看>>
sqlserver2005 递归查询
查看>>
30天提升技术人的写作力-第十一天
查看>>
OSPF环境下帧中继的配置
查看>>
Python 17.4 使用Web框架
查看>>
马哥1-3
查看>>
spring容器
查看>>
Linux系统架构(LB-HA集群)-nginx负载均衡集群配置
查看>>
ios版塔防类游戏源码
查看>>
Backup Exec 2010 V-79-57344-65072
查看>>
我的友情链接
查看>>