iOS 使用UIView的一种有效方法
在一个典型的MVC结构 中,Model部分负责保存目标数据,View部分主要负责实现数据的界面以及将数据显示出来,二者在Controller的操作下协同工作。在iOS应用中,View的实现主要由UIView及其派生类实现,主要由UILabel、UIImageView等等类来显示不同的信息。
这里展示一个demo来说明个人对UIView同数据交互的一种观点,个人意见仅供参考,欢迎讨论。
1、首先建立一个UIView的子类用于定制我们的视图对象
头文件:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#import <uikit uikit.h="">@interface UserInfoView : UIView//@property (nonatomic,copy) NSString *imgString;//@property (nonatomic,copy) NSString *nameString;//@property (nonatomic,copy) NSString *addrString;//@property (nonatomic,copy) NSString *infoString;//@property (nonatomic,copy) NSString *countString;//@property (nonatomic,copy) NSString *attString;//@property (nonatomic,copy) NSString *fansString;@property (nonatomic,retain) NSDictionary *param;- (void)loadData;@end</uikit> |
m文件:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#import "UserInfoView.h"#import "RectButton.h"@interface UserInfoView()//UI控件@property (nonatomic,retain) UIImageView *userImage;@property (nonatomic,retain) UILabel *nameLabel;@property (nonatomic,retain) UILabel *addressLabel;@property (nonatomic,retain) UILabel *infoLabel;@property (nonatomic,retain) UILabel *countLabel;@property (nonatomic,retain) RectButton *attButton;@property (nonatomic,retain) RectButton *fansButton;@property (nonatomic,retain) UIButton *profileButton;@property (nonatomic,retain) UIButton *moreButton;//数据成员//@property (nonatomic,copy) NSString *imgString;@property (nonatomic,copy) NSString *nameString;@property (nonatomic,copy) NSString *addrString;@property (nonatomic,copy) NSString *infoString;@property (nonatomic,copy) NSString *countString;//@property (nonatomic,copy) NSString *attString;//@property (nonatomic,copy) NSString *fansString;@end@implementation UserInfoView- (id)init{ CGRect frameRect = CGRectMake(0, 0, 320, 200); self = [self initWithFrame:frameRect]; if (self) { NSLog(@"Init called"); } return self;}- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) { self.backgroundColor = [UIColor lightGrayColor]; _userImage = [[UIImageView alloc] initWithFrame:CGRectZero]; [self addSubview:_userImage]; _nameLabel = [[UILabel alloc] initWithFrame:CGRectZero]; [self addSubview:_nameLabel]; _addressLabel = [[UILabel alloc] initWithFrame:CGRectZero]; [self addSubview:_addressLabel]; _infoLabel = [[UILabel alloc] initWithFrame:CGRectZero]; [self addSubview:_infoLabel]; _attButton = [[RectButton alloc] initWithFrame:CGRectZero]; [self addSubview:_attButton]; _fansButton = [[RectButton alloc] initWithFrame:CGRectZero]; [self addSubview:_fansButton]; _profileButton = [[UIButton alloc] initWithFrame:CGRectZero]; [self addSubview:_profileButton]; _moreButton = [[UIButton alloc] initWithFrame:CGRectZero]; [self addSubview:_moreButton]; _countLabel = [[UILabel alloc] initWithFrame:CGRectZero]; [self addSubview:_countLabel]; } return self;}- (void)setParam:(NSDictionary *)param{ _param = param; _nameString = [_param objectForKey:@"Name"]; _addrString = [_param objectForKey:@"Address"]; _infoString = [_param objectForKey:@"Infomation"]; _countString = [_param objectForKey:@"Count"]; [self loadData];}- (void)layoutSubviews{ _userImage.frame = CGRectMake(20, 20, 80, 80); _userImage.backgroundColor = [UIColor yellowColor]; _nameLabel.frame = CGRectMake(120, 20, 180, 20); _nameLabel.backgroundColor = [UIColor yellowColor]; _addressLabel.frame = CGRectMake(120, 50, 180, 20); _addressLabel.backgroundColor = [UIColor yellowColor]; _infoLabel.frame = CGRectMake(120, 80, 180, 20); _infoLabel.backgroundColor = [UIColor yellowColor]; _attButton.frame = CGRectMake(20, 110, 60, 60); _attButton.backgroundColor = [UIColor greenColor]; _fansButton.frame = CGRectMake(93, 110, 60, 60); _fansButton.backgroundColor = [UIColor greenColor]; _profileButton.frame = CGRectMake(167, 110, 60, 60); _profileButton.backgroundColor = [UIColor greenColor]; _moreButton.frame = CGRectMake(240, 110, 60, 60); _moreButton.backgroundColor = [UIColor greenColor]; _countLabel.frame = CGRectMake(20, 180, 280, 15); _countLabel.backgroundColor = [UIColor whiteColor]; [self loadData];}- (void)loadData{ if (self.nameString.length != 0) { _nameLabel.text = self.nameString; } if (self.addrString.length != 0) { _addressLabel.text = self.addrString; } if (self.infoString.length != 0) { _infoLabel.text = self.infoString; } if (self.countString.length != 0) { _countLabel.text = self.countString; }}@end |
在这个UserInfoView新建的时候,在initWithFrame中建立各个子视图,但是只是单纯新建一个对象而已,其frame设置为0。另 外,还重写了init函数,在函数中设置了指定的View大小,这样在Controller新建视图的时候不需要指定参数直接按照指定值进行操作。
UserInfoView中各个子视图的设置,在layoutSubView中完成,包括设置子视图的frame和背景颜色。layoutSubView函数可能经常被调用到,主要由以下几种情况:
· 当addSubView被调用时,被添加视图以及其子视图的layoutSubView会被调用;
· 当视图的frame发生改变时,会调用该视图的layoutSubView;
· 当滚动UIScrollView的时候会调用该视图及其父视图的layoutSubView;
· 旋转设备的时候;
· 向该视图发送setNeedLayout消息的时候。
2. 由Controller向View中发送数据
ViewController类的实现如下:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
#import "ViewController.h"#import "UserInfoView.h"@interface ViewController ()@property (nonatomic,retain) UIButton *People1;@property (nonatomic,retain) UIButton *People2;@property (nonatomic,retain) UserInfoView *userView;@end@implementation ViewController- (void)viewDidLoad{ [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. _userView = [[UserInfoView alloc] init]; [self.view addSubview:_userView]; _People1 = [UIButton buttonWithType:UIButtonTypeSystem]; _People1.frame = CGRectMake(20, 240, 120, 40); [_People1 setTitle:@"张三" forState:UIControlStateNormal]; _People1.backgroundColor = [UIColor lightGrayColor]; [_People1 addTarget:self action:@selector(setPeople1Data) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_People1]; _People2 = [UIButton buttonWithType:UIButtonTypeSystem]; _People2.frame = CGRectMake(180, 240, 120, 40); [_People2 setTitle:@"李四" forState:UIControlStateNormal]; _People2.backgroundColor = [UIColor lightGrayColor]; [_People2 addTarget:self action:@selector(setPeople2Data) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:_People2]; /* view.nameString = @"张三"; view.addrString = @"北京"; view.infoString = @"学生"; view.countString = @"12345"; view.nameString = @"李四"; view.addrString = @"上海"; view.infoString = @"工程师"; view.countString = @"54321";*/}- (void)setPeople1Data{ NSLog(@"setPeople1Data called."); NSDictionary *param = @{@"Name": @"张三", @"Address" : @"北京", @"Infomation" : @"学生", @"Count" : @"12345"}; _userView.param = param;}- (void)setPeople2Data{ NSLog(@"setPeople2Data called."); NSDictionary *param = @{@"Name": @"李四", @"Address" : @"上海", @"Infomation" : @"工程师", @"Count" : @"54321"}; _userView.param = param;}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end |
ViewController的默认视图上,分别实现了两个按钮并分别设置了响应函数。我们的目的是通过选择不同的按钮来改变UserInfoView中 显示的数据。从两个响应函数setPeople1Data和setPeople2Data的实现可知,UserInfoView所需要的信息都被封装在了一个字典型变量param中,对view的修改仅仅做了一个操作,即将该字典变量赋给了UserInfoView实例的一个property,通过这种改变一下目标view属性的方式即可完成对显示信息的更改。这样,Controller并不关心UserInfoView实例是如何解析字典参数的,也不需要对该实例进行其他操作,当需要更新数据的时候只需要一次赋值就可以了。如此可以最大程度地解除Controller和View的耦合性,提高代码的逻辑简洁度和可复用性。
再回到 UserInfoView类中的实现方法。如何实现在字典类property改变的同时对自己的子视图进行重写数据操作呢?方法很简单。首先将重写子视图数据的代码分离到loadData函数中,然后重写NSDictionary *param这个property的set方法(即setParam),然后在该set方法和layoutSubView方法中调用loadData方法就可以了。
iOS 使用UIView的一种有效方法的更多相关文章
- ios打包ipa的四种实用方法(.app转.ipa)
总结一下,目前.app包转为.ipa包的方法有以下几种: 1.Apple推荐的方式,即实用xcode的archive功能 Xcode菜单栏->Product->Archive->三选 ...
- 【原】ios打包ipa的四种实用方法(.app转.ipa)
总结一下,目前.app包转为.ipa包的方法有以下几种: 1.Apple推荐的方式,即实用xcode的archive功能 Xcode菜单栏->Product->Archive->三选 ...
- ios打包ipa的四种实用方法
总结一下,目前.app包转为.ipa包的方法有以下几种: 1.Apple推荐的方式,即实用xcode的archive功能 Xcode菜单栏->Product->Archive->三选 ...
- ios打包ipa的四种实用方法(.app转.ipa)-备
感谢大神分享这个博客 总结一下,目前.app包转为.ipa包的方法有以下几种: 1.Apple推荐的方式,即实用xcode的archive功能 Xcode菜单栏->Product->Arc ...
- iOS关闭键盘的两种简单方法
方法一: //1 [[[UIApplication sharedApplication] keyWindow] endEditing:YES]; ,为了关闭弹出的软键盘要遍历然后调用resig ...
- iOS 隐藏键盘的几种常见方法
1.设置return key,然后为Did End On Exit事件添加响应方法,并在方法内添加代码:[self.textfieldName resignFirstResponder]. 2.将背景 ...
- iOS Xcode注释的几种使用方法
1.#pragma mark - 方法分割线 2.#pragma mark 要备注的内容 3.// MARK: 要备注的内容 4.// FIXME: 要备注的内容 5.// TODO: 要备注的内容 ...
- IOS实现动画的几种简单方法
1.使用 NSTimer 来实现 [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(setNeed ...
- IOS 3种内省方法
IOS提供了3种内省方法 isKindOfClass 检查当前实例是否为某类及其子类 UIView *b = [UIView new]; //... id a = b; if ([a isMember ...
随机推荐
- Eclipse @override报错解决
第一种解决方案: @Override是JDK5 就已经有了,但有个小小的Bug,就是不支持对接口的实现,认为这不是Override 而JDK6 修正了这个Bug,无论是对父类的方法覆盖还是对接口的实现 ...
- SSH框架优缺点
SSH框架优缺点 开源是3个框架共有的优点 Struts2框架(MVC框架)的优点如下: 1) 实现了MVC模式,层次结构清晰,使程序员只需关注业务逻辑的实现: 2) 丰富的标签库,大大提高了开发 ...
- lightning mdb 源代码分析(5)-事务控制
本博文系列前面已经探讨了LMDB的系统架构.MMAP映射.B-Tree操作等部分,本文将尝试描述LMDB中的事务控制的实现. 事务的基本特征: 事务是恢复和并发控制的基本单位.它是一个操作序列,这些操 ...
- Android Studio 想说爱你不容易
开始使用Android Studio 真是非常痛苦的一段经历,而这一切的根源就在于GFW,俗称“墙” 如果避过墙来安装 AS,其实我已经在另外一篇文章中说明:http://www.cnblogs.co ...
- 【新产品发布】【EVC8001 磁耦隔离式 USB 转 RS-485】
EVC8001 是 XiaomaGee 团队打造的精品级 USB 转 RS-485 隔离转换器,全部采用最优方案,每个细节均做到最优化.最佳化.亮点举不胜举: ==================== ...
- php处理数组函数大全
PHP:指示支持该函数的最早的 PHP 版本. 函数 描述 PHP array() 创建数组. 3 array_change_key_case() 返回其键均为大写或小写的数组. 4 array_ch ...
- (6) 如何用Apache POI操作Excel文件-----POI-3.10的一个和注解(comment)相关的另外一个bug
如果POI-3.10往一个工作表(sheet)里面插入数据的话,需要注意了,其有一个不太被容易发现的bug. 被插入的工作表(sheet)里面的单元格没有包含任何的注解(comment)的时候,插入一 ...
- 12. 求简单交错序列前N项和
求简单交错序列前N项和 #include <stdio.h> int main() { int denominator, flag, i, n; double item, sum; whi ...
- osal_start_timerEx(Lock_TaskID,SBP_START_DEVICE_EVT,SBP_PERIODIC_EVT_PERIOD)的理解
osal_start_timerEx(Lock_TaskID,SBP_START_DEVICE_EVT,SBP_PERIODIC_EVT_PERIOD)与osal_set_event(Music_Ta ...
- 20145235 《Java程序设计》第8周学习总结
教材学习内容总结 15.1.1日志API简介 使用日志的起点是logger类,logger实例的创建有许多要处理的要素,必须使用logger的静态方法getLogger(). 通常在哪个类上取得的lo ...