我正在尝试在单独的文件中为 UIPickerView 创建一个委托和一个数据源。在 ViewController 我有:
- (void)viewDidLoad
{
[super viewDidLoad];
UIPickerView *myPickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 200, 320, 200)];
MonthPicker *monthPicker = [[MonthPicker alloc] init];
myPickerView.delegate = monthPicker;
myPickerView.dataSource = monthPicker;
myPickerView.showsSelectionIndicator = YES;
[self.view addSubview:myPickerView];
}
MonthPicker.h
#import <UIKit/UIKit.h>
@interface MonthPicker : NSObject<UIPickerViewDelegate,UIPickerViewDataSource>
@end
MonthPicker.m
#import "MonthPicker.h"
@implementation MonthPicker
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 7;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return @"yyy";
}
@end
目前我只使用占位符值来让委托工作。Everyting 编译正常,但是当我运行应用程序并转到其中包含选择器的视图时,它会在控制台中崩溃并显示 0 条错误消息。
我得到的唯一消息是:线程 1:EXC_BAD_ACCESS (code=2,address=0x0) 但这对我一点帮助都没有。
我还尝试用 NSLog 覆盖委托中的dealloc方法,似乎在崩溃发生之前,它被释放了,我觉得这很奇怪。
我错过了什么?
谢谢你。