UIPickerView selectRow:inComponent:animated not working in viewDidLoad but working in viewDidAppear

流过昼夜 提交于 2019-12-10 10:53:45

问题


This issue is driving me crazy. I have a UIPickerView that I loaded in viewDidLoad, and I need to initialize the selected row that is to appear on the screen. My code is as follows:

self.pickerSkins.delegate = self;
[self.pickerSkins reloadAllComponents];    
NSLog(@"%d", [self.pickerSkins numberOfRowsInComponent:0]);
[self.pickerSkins selectRow:1 inComponent:0 animated:NO];

I have put this in viewDidLoad. NSLog logs a value of 2. However, the selected row that pops up is always the first row and not the second.

If I put the selectRow in viewWillAppear, it also doesn't work.

If I put the selectRow in viewDidAppear, the PickerView appears and then goes to the second row.

UPDATE: If I have more than 2 rows, the method works correctly from viewDidLoad, unless I am scrolling to the last row. The method never works in viewDidLoad or viewWillAppear if I am scrolling to the last row. Any idea why this is? Thanks.

How do I get the selected row to be the second row before the view appears?

Thanks.

来源:https://stackoverflow.com/questions/16114054/uipickerview-selectrowincomponentanimated-not-working-in-viewdidload-but-worki

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!