UIPickerViewDelegate方法未被调用

时间:2012-06-08 19:57:51

标签: objective-c ios uitableview uipickerview uipickerviewcontroller

我创建了一个包含UIPickerViewUITableViewCell的自定义视图,以显示当前在选择器中选择的内容。我的PickerViewController设置为选择器的delegatedataSource,我已实施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";
    }
}

Picker查看数据源方法

- (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;
}

Picker View委托方法

- (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;
}

1 个答案:

答案 0 :(得分:2)

如果你的numberOfRows ... datasource方法返回0,那么你的委托方法永远不会被调用 - 如果它不认为它有任何要显示的行,那么选择器将不会要求行的标题。