Skip to content

Commit 9a6b1cb

Browse files
committed
T3:Nib file
1 parent 937d5fe commit 9a6b1cb

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

_posts/2015-12-05-Nib-load-process.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ title: iOS中nib文件的加载过程
99
- (NVClientAccountViewCell *)loadCell{
1010
NSArray *views = [[NSBundle mainBundle] loadNibNamed:@"NVClientAccountViewCell" owner:nil options:nil];
1111
return (NVClientAccountViewCell *)[views objectAtIndex:0];
12-
} {% endhighlight %}
12+
}
13+
{% endhighlight %}
1314

1415
利用这段代码通过nib文件构造了TableViewCell。
1516

@@ -31,18 +32,19 @@ action连接: 利用UIControl对象的addTarget:action:forControlEvents:方法
3132

3233
#### 在nib文件中定义IBOutlet的三种方式
3334

34-
1. 将Outlet定义为@property,放在h文件中。
35+
- 将Outlet定义为@property,放在h文件中。
36+
3537
不推荐,该方法将IBOutlet完全暴露出来,不符合封装的要求。
3638

37-
2. 将Outlet定义为@property,通过category方式使其成为类的私有变量,放在m文件中。
39+
- 将Outlet定义为@property,通过category方式使其成为类的私有变量,放在m文件中。
3840
{% highlight objective-c %}
3941
@interface MyViewCell()
4042
@property (weak, nonatomic) IBOutlet UILabel *label;
4143
@property (weak, nonatomic) IBOutlet UIImageView *image;
4244
@end
4345
{% endhighlight %}
4446

45-
3. 直接将Outlet设置成为类的私有变量。
47+
- 直接将Outlet设置成为类的私有变量。
4648
{% highlight objective-c %}
4749
@implementation MyViewCell{
4850
IBOutlet UILabel *titleLbl;
@@ -55,7 +57,8 @@ action连接: 利用UIControl对象的addTarget:action:forControlEvents:方法
5557

5658
- (void)setIcon:(NSString*) theIcon{
5759
iconImg.image = [UIImage imageNamed:theIcon];
58-
}{% endhighlight %}
60+
}
61+
{% endhighlight %}
5962

6063
在加载nib文件过程中,重建outlet的时候,会调用outlet的getter和setter方法初始化outlet。如果在方法三中,将UILabel的名字设成title,就会将setTitle方法当成title重载后的setter方法,用它来初始化UILabel,造成outlet初始化失败,为nil。
6164

0 commit comments

Comments
 (0)