1.模型对象
所谓模型,其实就是数据模型,专门用来存放数据的对象,用它来表示数据会更加专业
模型设置数据和取出数据都是通过它的属性,属性名如果写错了,编译器会马上报错,因此,保证了数据的正确性
使用模型访问属性时,编译器会提供一系列的提示,提高编码效率
字典转模型的过程最好封装在模型内部
模型应该提供一个可以传入字典参数的构造方法
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)xxxWithDict:(NSDictionary *)dict;
2. instancetype
instancetype在类型表示上,跟id一样,可以表示任何对象类型
instancetype只能用在返回值类型上,不能像id一样用在参数类型上
instancetype比id多一个好处:编译器会检测instancetype的真实类型
3.Xib文件的加载
Xib文件可以用来描述某一块局部的UI界面
方法1
//通过xib文件创建view
NSBundle*bundle = [NSBundlemainBundle];
// 读取xib文件(会创建xib中的描述的所有对象,并且按顺序放到数组中返回)
NSArray*objs = [bundleloadNibNamed:@"JNAppView"owner:nil options:nil];
JNAppView*appView = [objslastObject];
这个方法会创建xib中的所有对象,并且将对象按顺序放到objs数组中
方法2
bundle参数可以为nil,默认就是main bundle
UINib *nib = [UINibnibWithNibName:@"JNAppView"bundle:[NSBundlemainBundle]];
NSArray *objs = [nibinstantiateWithOwner:niloptions:nil];
在开发阶段,面向开发者的是xib文件; 当把应用装到手机上时,xib文件就会转为nib文件
4.view的封装
如果一个view内部的子控件比较多,一般会考虑自定义一个view,把它内部子控件的创建屏蔽起来,不让外界关心
外界可以传入对应的模型数据给view,view拿到模型数据后给内部的子控件设置对应的数据
5.UILabel的常见设置
@property(nonatomic,copy)NSString *text;
显示的文字
@property(nonatomic,retain)UIFont *font;
字体
@property(nonatomic,retain)UIColor *textColor;
文字颜色
@property(nonatomic)NSTextAlignment textAlignment;
对齐模式(比如左对齐、居中对齐、右对齐)
6.UIFont
UIFont代表字体,常见创建方法有以下几个:
+ (UIFont *)systemFontOfSize:(CGFloat)fontSize; 系统默认字体
+ (UIFont *)boldSystemFontOfSize:(CGFloat)fontSize; 粗体
+ (UIFont *)italicSystemFontOfSize:(CGFloat)fontSize; 斜体
7.UIButton的常见设置
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
设置按钮的文字
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
设置按钮的文字颜色
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
设置按钮内部的小图片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
设置按钮的背景图片
btn.titleLabel.font = [UIFontsystemFontOfSize:13];
设置按钮的文字字体(需要拿到按钮内部的label来设置)
- (NSString *)titleForState:(UIControlState)state;
获得按钮的文字
- (UIColor *)titleColorForState:(UIControlState)state;
获得按钮的文字颜色
- (UIImage *)imageForState:(UIControlState)state;
获得按钮内部的小图片
- (UIImage *)backgroundImageForState:(UIControlState)state;
获得按钮的背景图片
/设置imageView中的image样式为自适应
iconImage.contentMode = UIViewContentModeScaleAspectFit;
//设置label中的文字为居中
nameLabel.textAlignment = NSTextAlignmentCenter;
//设置按钮中文字的字体
downloadBtn.titleLabel.font= [UIFont systemFontOfSize:12];
#pragma mark - 懒加载,将字典转为模型对象
- (NSArray *)apps
{
if (_apps == nil) {
// 初始化
// 1.获得plist的全路径
NSString *path = [[NSBundle mainBundle] pathForResource:@"app.plist" ofType:nil];
// 2.加载数组
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
// 3.将dictArray里面的所有字典转成模型对象,放到新的数组中
NSMutableArray *appArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
// 3.1.创建模型对象
// JNApp *app = [[JNApp alloc] initWithDict:dict];
JNApp *app = [JNApp appWithDict:dict];
// 3.2.添加模型对象到数组中
[appArray addObject:app];
}
// 4.赋值
_apps = appArray;
}
return _apps;
}
//代码规范 : 一般提供两个方法,一个构造方法和一个以类名开头的类方法
#pragma mark - 在模型对象中对传入的字典进行封装
- (instancetype)initWithDict:(NSDictionary *)dict
{
if (self = [super init]) {
self.name = dict[@"name"];
self.icon = dict[@"icon"];
}
return self;
}
+ (instancetype)appWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}