UIPicker noob 问题 - 如果我单击选择器中的第一个项目,结果为 null - 所有其他项目都很好

发布于 2024-10-19 02:31:16 字数 1361 浏览 7 评论 0原文

我有一个 UIPicker,里面有 7 个项目。 (只有一个组件)...如果我单击选择器中的第一项(没有选择器的任何滚动),我得到的文本结果为空。

但是,如果我在列表中向下“拖拽”,然后单击第 1 项,它就可以正常工作。

如果我单击任何其他项目,它总是返回正确的结果。当然,列表必须滚动才能选择这些项目。

到底发生了什么导致这种情况?我需要调用委托函数吗?如果他们从不单击任何内容,是否有某种方法可以默认选择列表中的第一项?

谢谢,

Phil

这是我的 .m 中“viewdidload”的片段

    - (void)viewDidLoad {
        [super viewDidLoad];

        NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

        listOfTerms=[[NSArray alloc]initWithObjects:
                     @"Fall Semester",@"Spring Semester",@"Summer Session",@"Q1",@"Q2",@"Q3",@"Q4",nil];
...
...

这是我定义的其他 4 个 UIPickerView 委托函数。

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}




- (NSInteger)pickerView:(UIPickerView *)pickerView 
numberOfRowsInComponent:(NSInteger)component {
        return [listOfTerms count];

}





-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
        return [listOfTerms objectAtIndex:row];
}





- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row 
       inComponent:(NSInteger)component {

    selectedTerm =[[NSString alloc] 
                       initWithFormat:@"%@",
                       [listOfTerms objectAtIndex:row]];

}

I have a UIPicker with 7 items in it. (only one component)... If I click on the first item in the Picker (without ANY SCROLLING of the picker), the text results I get back are null.

However, If I so much as "tug" downward on the list and THEN CLICK item #1 it works fine.

If I click ANY other item, it always returns the right result. Of course, the list has to scroll in order for those items to get selected.

What is happening to cause this? Is there a delegate function I need to call? Is there some way to default the selection to the first item in the list, if they never click anything?

Thanks,

Phil

Here is a snippet from "viewdidload" in my .m

    - (void)viewDidLoad {
        [super viewDidLoad];

        NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

        listOfTerms=[[NSArray alloc]initWithObjects:
                     @"Fall Semester",@"Spring Semester",@"Summer Session",@"Q1",@"Q2",@"Q3",@"Q4",nil];
...
...

Here are the other 4 UIPickerView delegate functions I have defined.

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}




- (NSInteger)pickerView:(UIPickerView *)pickerView 
numberOfRowsInComponent:(NSInteger)component {
        return [listOfTerms count];

}





-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
        return [listOfTerms objectAtIndex:row];
}





- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row 
       inComponent:(NSInteger)component {

    selectedTerm =[[NSString alloc] 
                       initWithFormat:@"%@",
                       [listOfTerms objectAtIndex:row]];

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

等风来 2024-10-26 02:31:16

pickerView:didSelectRow:inComponent: 仅在选取器视图滚动时调用。因此,如果您从不滚动视图,则永远不会调用该方法,因此永远不会设置 selectedTerm。修复很简单,在 viewDidLoad: 中初始化 selectedTerm。

pickerView:didSelectRow:inComponent: is only called when the picker view scrolls. So if you never scroll the view, the method never gets called and therefore selectedTerm is never set. The fix is simple, initialize selectedTerm in viewDidLoad:.

蔚蓝源自深海 2024-10-26 02:31:16

对于所有情况,最简单的答案是在 viewDidAppear 中暗示 didSelectRow

(请注意,它不是 viewDidLoad 方法)

- (void) viewDidAppear:(BOOL)animated{ 
    [self pickerView:[self yourpickerview] didSelectRow:0 inComponent:0;
}

The most simple answer for all situation is to imply didSelectRow in viewDidAppear

(Please note that it is NOT viewDidLoad method)

- (void) viewDidAppear:(BOOL)animated{ 
    [self pickerView:[self yourpickerview] didSelectRow:0 inComponent:0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文