I am working on a Simple Slot Machine. I Sub-Classed from UIPickerView. The PickerView DataSource I don’t need to define in my controller, because it will always be 5. Can I define it in my subclass like so?
@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
Yes, you can. You can also just write
self.dataSource = self, becausesuperandselfare the same object*. Remember, your object is just as much an instance of its superclass as it is an instance of the class it’s declared as. It has all the same properties and responds to all the same messages.*The only difference between
superandselfis thatsuperskips over methods defined in the current class.