Click the UIPickerView listener button.
We use pickview when selecting a region, for example. When you use it, select the corresponding region and click OK or cancel to end the editing. However, if we do this:
UIPickerView * pick = [[UIPickerView alloc] init];
[Pick addSubview: cancel_btn];
[Pick addSubview: OK _btn];
Effect:
When you click the button, it is found that the click event is listened by pickview, and the button cannot receive the event. Therefore, this method is unavailable.
Of course, you can also click on the area selection, we will pop up a mask, then add pickview, cancel_btn and OK _btn to the mask, and then click the listener button to pass the selected data to the Controller. this method is feasible, but it is difficult to think about it and is not recommended.
We recommend that you use the following method:
Remember that UITextField has an inputView attribute, which controls the view popped up by clicking field. What we need to do is field. inputView = pickView;
The point is, how do I put the buttons? We define a UITextField. The field has an attribute called inputAccessoryView, which adds an auxiliary View to the field pop-up view, at this time, we only need to add the button to this secondary view.
UIToolbar * tool = [[UIToolbar alloc] init];
Tool. frame = CGRectMake (0, 0, 0, 44 );
[Tool addSubview: cancel_btn];
[Tool addSubview: OK _btn];
Field. inputAccessoryView = tool;
You only need to add the button in the layoutSubviews method.
Then the effect is as follows: