@@ -9,7 +9,8 @@ title: iOS中nib文件的加载过程
9
9
- (NVClientAccountViewCell * )loadCell{
10
10
NSArray * views = [[ NSBundle mainBundle] loadNibNamed:@"NVClientAccountViewCell" owner: nil options: nil ] ;
11
11
return (NVClientAccountViewCell * )[ views objectAtIndex:0] ;
12
- } {% endhighlight %}
12
+ }
13
+ {% endhighlight %}
13
14
14
15
利用这段代码通过nib文件构造了TableViewCell。
15
16
@@ -31,18 +32,19 @@ action连接: 利用UIControl对象的addTarget:action:forControlEvents:方法
31
32
32
33
#### 在nib文件中定义IBOutlet的三种方式
33
34
34
- 1 . 将Outlet定义为@property ,放在h文件中。
35
+ - 将Outlet定义为@property ,放在h文件中。
36
+
35
37
不推荐,该方法将IBOutlet完全暴露出来,不符合封装的要求。
36
38
37
- 2 . 将Outlet定义为@property ,通过category方式使其成为类的私有变量,放在m文件中。
39
+ - 将Outlet定义为@property ,通过category方式使其成为类的私有变量,放在m文件中。
38
40
{% highlight objective-c %}
39
41
@interface MyViewCell()
40
42
@property (weak, nonatomic) IBOutlet UILabel * label;
41
43
@property (weak, nonatomic) IBOutlet UIImageView * image;
42
44
@end
43
45
{% endhighlight %}
44
46
45
- 3 . 直接将Outlet设置成为类的私有变量。
47
+ - 直接将Outlet设置成为类的私有变量。
46
48
{% highlight objective-c %}
47
49
@implementation MyViewCell{
48
50
IBOutlet UILabel * titleLbl;
@@ -55,7 +57,8 @@ action连接: 利用UIControl对象的addTarget:action:forControlEvents:方法
55
57
56
58
- (void)setIcon:(NSString* ) theIcon{
57
59
iconImg.image = [ UIImage imageNamed: theIcon ] ;
58
- }{% endhighlight %}
60
+ }
61
+ {% endhighlight %}
59
62
60
63
在加载nib文件过程中,重建outlet的时候,会调用outlet的getter和setter方法初始化outlet。如果在方法三中,将UILabel的名字设成title,就会将setTitle方法当成title重载后的setter方法,用它来初始化UILabel,造成outlet初始化失败,为nil。
61
64
0 commit comments