问题
I have a 2 components, dependent pickerView
. I have a preview UIImageView
that changes each time the user uses the pickerView
(and triggers the didSelectRow:inComponent:
). This works just as I expected.
However, I want the pickerView
to trigger didSelectRow:
when the app launches, so that there is a Preview the moment the user sees the UIImageView
. In viewDidLoad
, when I try this:
[self pickerView:picker didSelectRow:row inComponent:component]; // row and component have 0 values.
next thing that happens - the pickerView
shows up blank! Remove this line, and it works perfectly again. I tried this as well:
[picker selectRow: row inComponent:component animated:NO];
thinking that it will trigger didSelectRow:
however it doesn't trigger it and my UIImageView
remain blank.
Any advice on how to call the didSelectRow:
method without the users help?
Update: by the way, the picker is inside another UIView
(not the main one). In case it matters.
回答1:
didSelectRow:...
will only be called if a row has been selected by user interaction. If you select a row programmatically with selectRow:...
then you have to add you own logic to update other components like the image view.
The principle is valid for many other delegate functions, such as e.g. tableView:didSelectRowAtIndexPath:
.
回答2:
Based on your comments, this what you are looking for then. Does UIPickerView's selectRow:inComponent:animated: call pickerView:didSelectRow:inComponent:?
You have to manually call the didSelectRow
method.
来源:https://stackoverflow.com/questions/13075474/how-to-call-pickerview-didselectrow-incomponent-without-user-interaction