我创建了一个自定义视图,其中包含 aUIPickerView
和 aUITableViewCell
以显示选择器中当前选择的内容。MyPickerViewController
被设置为选择器的delegate
和dataSource
,并且我已经实现了numberOfComponentsInPickerView:
、pickerView: numberOfRowsInComponent:
和pickerView: titleForRow: forComponent:
,但是当程序运行时选择器显示为空白,并且由于NSLog
在方法中放置了调用,我发现这些方法永远不会被调用. 我不知道问题是什么,因为选择器似乎与PickerViewController
.
对于我想要实现的基本功能,请查看 iOS 日历应用程序中的“开始和结束”视图。我试图准确地模仿这一点,但只使用一个UITableViewCell
而不是三个。
PickerViewController.h
#import <UIKit/UIKit.h>
@interface PickerViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITableViewCell *pickerSelection;
// Property to dynamically determine what content to display in the picker.
@property (nonatomic) BOOL userIsChoosingClass;
@end
viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(self.thePicker.delegate.description);
if (self.userIsChoosingClass) {
self.pickerSelection.textLabel.text = @"Class";
} else {
self.pickerSelection.textLabel.text = @"Major";
}
}
选取器视图数据源方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
size_t numberOfRows = 0;
if (self.userIsChoosingClass) {
numberOfRows = [self.brain classChoicesForSignUp].count;
} else {
numberOfRows = [self.brain majorChoicesForSignUp].count;
}
return numberOfRows;
}
选择器视图委托方法
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
NSLog(@"Picker view delegate method called");
NSString *title;
if (self.userIsChoosingClass) {
title = [[self.brain classChoicesForSignUp] objectAtIndex:row];
} else {
title = [self.majorChoices objectAtIndex:row];
}
return title;
}