控制器属性传值的一些小问题
控制器属性传值的一些小问题
这篇博文并没有什么技术含量,属于很基础的知识,但也容易在感官上产生错误的认识,今天记录于此。
一个对象,无论是被转存到数组中,还是被多个控制器持有,如果这个对象没有发生值的拷贝(创建出新的对象),所有的对这个对象的持有者只持有了一个样本,修改了这个样本的值会影响到所有持有者。
效果演示图:
测试用源码:
DataModel.h 与 DataModel.m
// // DataModel.h // BaseViewController // // Created by YouXianMing on 15/1/2. // Copyright (c) 2015年 YouXianMing. All rights reserved. // #import <Foundation/Foundation.h> @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 <UIKit/UIKit.h> #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的值,就会影响到全局的值,这点需要注意哦。