首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIPickerViewDelegate方法未被调用

UIPickerViewDelegate方法未被调用
EN

Stack Overflow用户
提问于 2012-06-08 19:57:50
回答 1查看 2.9K关注 0票数 0

我已经创建了一个自定义视图,该视图包含一个UIPickerView和一个UITableViewCell,以显示当前在选择器中选择的内容。我的PickerViewController被设置为选择器的delegatedataSource,我已经实现了numberOfComponentsInPickerView:pickerView: numberOfRowsInComponent:pickerView: titleForRow: forComponent:,但是当程序运行时,选择器看起来是空的,由于在方法中放置了NSLog调用,我发现这些方法永远不会被调用。我不知道问题是什么,因为选择器似乎与PickerViewController连接正确。

关于我正在尝试实现的基本功能,请查看iOS日历应用程序中的"Start & End“视图。我正试图准确地模仿这一点,但只使用一个UITableViewCell而不是三个。

PickerViewController.h

代码语言:javascript
复制
#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

代码语言:javascript
复制
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(self.thePicker.delegate.description);
    if (self.userIsChoosingClass) {
        self.pickerSelection.textLabel.text = @"Class";
    } else {
        self.pickerSelection.textLabel.text = @"Major";
    }
}

选择器视图数据源方法

代码语言:javascript
复制
- (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;
}

选择器视图代理方法

代码语言:javascript
复制
- (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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-11 17:21:12

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10955323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档