我有UITextField
,推送时UIPickerView
出现以选择一个值。一旦选择了值,如何让UIPickerView
解散。另一个帖子中有人告诉我resignFirstResponder
文本字段,但我的代码无效。有任何想法吗?注意:我有两个文本字段和UI选择器,这就是为什么我有“if”'else'语句。
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.ageTextField)
{
[ageTextField resignFirstResponder];
[agePickerView removeFromSuperview];
return YES;
}
else
{
[relationshipTextField resignFirstResponder];
[relationshipPickerView removeFromSuperview];
return YES;
}
}
答案 0 :(得分:0)
尝试使用以下代码来解决您的问题。
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == self.ageTextField)
{
[ageTextField resignFirstResponder];
}
else
{
[relationshipTextField resignFirstResponder];
}
[pickerView removeFromSuperview];
return YES;
}
或按照UIPickerView
的以下方法重新设置键盘。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
答案 1 :(得分:0)
请尝试在UIPickerViewDelegate方法中重新签名UITextField,如下所示。
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
[self.ageTextField resignFirstResponder];
pickerView.hidden = YES; //Show the pickerview again when you neeed it
}
答案 2 :(得分:0)
实现UIPickerViewDelegate协议并实现以下方法:
- (void) pickerView: (UIPickerView*) pickerView
didSelectRow: (NSInteger) row
inComponent: (NSInteger) component
{
[self.ageTextField resignFirstResponder];
}