iOS7的一些总结:使用UIView的一种有效方法

本文介绍了一个iOS应用中采用MVC模式的具体实现案例。通过创建自定义UIView子类UserInfoView来展示用户信息,并演示了如何通过Controller向View传递数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在一个典型的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

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方法就可以了。

转载地址:http://www.cocoachina.com/ios/20140924/9740.html

内容概要:本文档提供了关于“微型车间生产线的设计与生产数据采集试验研究”的毕业设计复现代码,涵盖从论文结构生成、机械结构设计、PLC控制系统设计、生产数据采集与分析系统、有限元分析、进度管理、文献管理和论文排版系统的完整实现。通过Python代码和API调用,详细展示了各个模块的功能实现和相互协作。例如,利用SolidWorks API设计机械结构,通过PLC控制系统模拟生产流程,使用数据分析工具进行生产数据的采集和异常检测,以及利用进度管理系统规划项目时间表。 适合人群:具有机械工程、自动化控制或计算机编程基础的学生或研究人员,尤其是从事智能制造领域相关工作的人员。 使用场景及目标:①帮助学生或研究人员快速搭建和理解微型车间生产线的设计与实现;②提供完整的代码框架,便于修改和扩展以适应不同的应用场景;③作为教学或科研项目的参考资料,用于学习和研究智能制造技术。 阅读建议:此资源不仅包含详细的代码实现,还涉及多个学科领域的知识,如机械设计、电气控制、数据分析等。因此,在学习过程中,建议读者结合实际操作,逐步理解每个模块的功能和原理,并尝试调整参数以观察不同设置下的系统表现。同时,可以参考提供的文献资料,深入研究相关理论和技术背景。
本次的学生体质健康信息管理网站,按照用户的角色可以分为教师与学生,后台设置管理员角色来对学生的信息进行管理。,设计如下: 1、后台管理系统 后台管理系统主要是为该系统的管理员提供信息管理服务的系统,具体包括的功能模块如下: (1)管理员信息管理 (2)教师信息管理 (3)学生信息管理 (4)健康信息统计(图形化进行健康,亚健康等学生的信息数量统计) 2、教师角色的功能模块设计 教师角色所需要的功能模块主要包括了如下的一些内容: (1)个人资料修改 (2)学生体质健康管理:录入相关数据,包括但不限于身高、体重、肺活量、视力等生理指标以及运动能力、身体成分、骨密度等健康指标,并且设置健康,亚健康状态 (3)学生健康建议:根据体质信息,进行学生健康的建议 (4)健康预警:对健康出问题的学生,进行健康预警 (5)饮食和锻炼情况管理,查看 3、学生角色 学生角色可以通过该信息网站看到个人的基本信息,能够看到教师给与学生的健康建议等,功能模块设计如下: (1)个人资料修改 (2)我的健康建议查看 (3)我的健康预警 (4)饮食和锻炼情况管理,记录平时的饮食和锻炼情况 完整前后端源码,部署后可正常运行! 环境说明 开发语言:Java后端 框架:ssm,mybatis JDK版本:JDK1.8+ 数据库:mysql 5.7+ 数据库工具:Navicat11+ 开发软件:eclipse/idea Maven包:Maven3.3+ 部署容器:tomcat7.5+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值