我做了一個自定義視圖。
我設置兩個啓動方式,
1. initWithFrame(代碼初始化)
2的initWithCoder(對於情節串連圖板初始化)設置自定義類的代理對象
在我的自定義類
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// [self setUp] method contain the code to run the delegate.
[self setUp];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self) {
// [self setUp] method contain the code to run the delegate.
[self setUp];
self.backgroundColor = [UIColor greenColor];
}
return self;
}
然後,我添加視圖來故事板並在身份檢查員處更改課程。 我將視圖連接到viewcontroller屬性。
我設置委託和數據源
在我的視圖控制器
@interface ViewController() <UIControlViewDataSource, UIControlViewDelegate>
@property (weak, nonatomic) IBOutlet UIControlView *controlView;
@end
- (void)viewDidLoad
{
self.controlView.delegate = self;
self.controlView.dataSource = self;
}
的的initWithCoder在viewDidLoad中運行前,
因爲它的代表將永遠不會因爲的initWithCoder在運行我自定義類正在運行,然後在我的視圖控制器中設置viewDidLoad中的委託。
而委託屬性將具有NULL值。
我該如何設置代表?
爲什麼你認爲在設置委託之前沒有initWithCoder應該調用? – Pawan 2014-08-27 20:50:29
@pawan initWithCoder運行[self setUp]方法,該方法包含從委託方法中彙集數據的代碼,如果委託尚未設置,則該方法無法運行 – 2014-08-28 10:08:07