UIPicker 选择行

发布于 2024-09-15 02:25:33 字数 562 浏览 8 评论 0原文

我正在尝试选择第一行作为默认值.. 在这里,我尝试了

-(void)pickerViewLoaded{

   [pickerView setShowSelectionIndicator:YES];
   pickerView.delegate = self;
   [pickerView reloadAllComponents];
   [pickerView selectRow:1 inComponent:0 animated:YES]
}

当我在 viewDidLoad() 上调用此方法(它调用 didSelectRow 方法)时效果很好,但它选择的是该组件的第二行而不是第一行。 当我使用 [pickerView selectRow:0 inComponent:0animated:YES] 调用时,它永远不会调用 didSelectRow 方法。

另一个问题是当我在选择分段控件时调用 pickerViewLoaded 方法时,它不会调用 didSelectRow 方法。我仍然不知道什么时候它可以使用viewDidLoad调用,那么为什么不使用segmentedControl呢?

谢谢大家,

I am trying to select the first row as default..
Here i tried

-(void)pickerViewLoaded{

   [pickerView setShowSelectionIndicator:YES];
   pickerView.delegate = self;
   [pickerView reloadAllComponents];
   [pickerView selectRow:1 inComponent:0 animated:YES]
}

It works fine when i call this method on viewDidLoad() which call didSelectRow method but it is selecting the second row of that component not first one.
when i call with [pickerView selectRow:0 inComponent:0 animated:YES] then it never call didSelectRow method.

another problem is when i call pickerViewLoaded method on selection of segmented control it does not call the didSelectRow method. i still could not figure when it may call with viewDidLoad then why not with segmentedControl.

Thanks all,

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

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

发布评论

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

评论(3

旧街凉风 2024-09-22 02:25:33

似乎是一个老线程,无论如何我想分享我的经验。

我遇到了同样的问题,并

[pickerView selectRow:0 inComponent:0 animated:YES];

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

仅在使用通知返回最后一行调用的 titleForRow 后才将其移至我调用 selectRow:inComponent:animated

seems to be a old thread, anyway I thought of sharing my experience.

I had the same issue and moved the

[pickerView selectRow:0 inComponent:0 animated:YES];

to

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

I call the selectRow:inComponent:animated only after returning the titleForRow called for last-row using a notification.

淑女气质 2024-09-22 02:25:33

现在问题已经从死里复活了...

我认为 selectRow 使用从 0 开始的索引,而不是 1。所以 0 是第一行,1 是第二行。

Now that the question has been raised from the dead...

I think selectRow uses an index that starts at 0 - not 1. So 0 is the first row, and 1 is the second row.

森林很绿却致人迷途 2024-09-22 02:25:33

我刚刚遇到了同样的问题并解决了这个问题:如果所选行没有更改,则不会调用委托。此外,默认情况下会选择第 0 行,因此在 viewDidLoad 中重新选择第 0 行不会触发委托。就我而言,我能够直接调用委托方法,也许这也是您的情况的解决方案。

I just ran into the same problem and figured it out: The delegate doesn't get called, if the selected row doesn't change. Also, row 0 is selected by default, so re-selecting row 0 in viewDidLoad doesn't trigger the delegate. In my case I was able to call the delegate method directly, maybe that's the solution in your case, too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文