调用SuperClass的DataSource?

时间:2011-06-30 19:34:19

标签: iphone objective-c ios4

我正在研究一台简单的老虎机。我从UIPickerView Sub-Classed。 PickerView DataSource我不需要在我的控制器中定义,因为它总是为5.我可以在我的子类中定义它吗?

@interface SlotMachineView : UIPickerView <UIPickerViewDataSource> {

}

@end

#import "SlotMachineView.h"

@implementation SlotMachineView

- (id)init{

if ((self = [super init])) {
    super.dataSource = self;
}

return self;
}


- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

return 5;
}


- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

return 5;
}

- (void)dealloc
{ 
[super dealloc];
}

@end

2 个答案:

答案 0 :(得分:2)

是的,你可以。您也可以只写self.dataSource = self,因为superself是同一个对象*。请记住,您的对象与其超类的实例一样,因为它是它声明为的类的实例。它具有所有相同的属性,并响应所有相同的消息。

* superself之间的唯一区别是super跳过了当前类中定义的方法。

答案 1 :(得分:0)

是的,你可以这样做。这就是协议背后的重点。