Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/98.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS-can';我无法摆脱键盘_Ios_Keyboard - Fatal编程技术网

iOS-can';我无法摆脱键盘

iOS-can';我无法摆脱键盘,ios,keyboard,Ios,Keyboard,我有一个文本字段供用户输入字符串,然后另一个文本字段在单击时显示一个视图,其中有一个日期选择器 如果用户单击第一个文本字段并使用键盘输入字符串,然后单击键盘上的返回按钮,键盘将消失。但是,如果他们在第一个文本字段中输入字符串后单击日期文本字段,则键盘将保持在屏幕上 我试过这个: @property (weak, nonatomic) IBOutlet UITextField *nameField; @property (weak, nonatomic) IBOutlet UITextField

我有一个文本字段供用户输入字符串,然后另一个文本字段在单击时显示一个视图,其中有一个日期选择器

如果用户单击第一个文本字段并使用键盘输入字符串,然后单击键盘上的返回按钮,键盘将消失。但是,如果他们在第一个文本字段中输入字符串后单击日期文本字段,则键盘将保持在屏幕上

我试过这个:

@property (weak, nonatomic) IBOutlet UITextField *nameField;
@property (weak, nonatomic) IBOutlet UITextField *dateField;

....

- (BOOL) textFieldShouldBeginEditing:(UITextField *)textField {
    return(![textField isEqual:self.dateField]);
}
它不起作用。 我也尝试过这一点(当用户单击dateField时,在开始编辑时触发):

然后这个:

- (IBAction)dateTextFieldClicked:(id)sender {
    [self.view endEditing:YES];
}    
这两种方法都不管用。我想做的事可能吗?我应该放弃,还是有办法奏效

试试这个

- (IBAction)dateTextFieldClicked:(id)sender {
[self.nameField resignFirstResponder];
[self.view endEditing:YES]; }    

在您的
viewDidLoad()中尝试以下操作:

并将
dataTextFieldClicked()中的参数更正为

 - (IBAction)dateTextFieldClicked:(UITextField *)sender {
    [sender resignFirstResponder];    
}
当苹果推出
Swift
时,我只是一名iOS开发者,这意味着我上面写的代码可能有一些语法错误,但没有逻辑错误


我希望我的回答能帮助你。

有一个简单的方法,我也有同样的问题。将日期字段作为UIButton,而不是UIExtField。在按钮操作中,设置如下:

- (IBAction)Date_set:(id)sender
 {
    [self.view endEditing:YES];
    [self YourDatePickerset];
} 
当DatePicker值像这样更改时,设置按钮的标题

- (void)pickerChanged:(id)sender
{
    NSDateFormatter *dateformate=[[NSDateFormatter alloc]init];
    [dateformate setDateFormat:@"dd LLL yyyy"]; // Date formater
    NSString *date = [dateformate stringFromDate:[myPicker date]]; // Convert date to string
    NSLog(@"date :%@",date);
    [dob_btn setTitle:date forState:UIControlStateNormal];
}

你能用这个片段告诉我结果吗

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
确保为所有文本字段将委托设置为self

也试试这个
编辑

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[dateField resignFirstResponder];    
[nameField resignFirstResponder];
}

当你的键盘没有关闭时,会有点困惑。你能提供一个屏幕截图来进一步澄清吗@ManuelI将其正确地翻译为objective-c,但它不起作用。因此,它将是[self.nameTextField addTarget:self action:selector@(nameFieldClicked:)forControlEvents:UIControlEventEditingDidEnd];-(iAction)dateTextFieldClicked:(id)发送者{UITextField*name=(UITextField*)发送者;[name resignFirstResponder];}更正你的
dataTextFieldClicked()
[sender resignFirstResponder]
当我完成Swift绑定时,我使用此方法块将
UITextField
从屏幕中删除。区别在于我在
连接检查器中使用
发送事件
设置
情节提要中的UI对象,而不是
添加目标()
。如果使用
情节提要
,则可以将
UITextField
编辑结束
连接到
dataTextFieldClicked()
。和
-(iAction)dataTextFieldClicked()
的返回值应该是
void
,就像
-(void)dataTextFieldClicked()
@manuelmanningtonThird您在界面中添加了uitextfielddelegate并为两个文本字段设置了委托吗?@manuelmanningtonThird那么为什么不尝试委托方法呢?您可以使用我在下面提到的委托方法:-(void)textfielddebeginediting:(UITextField*)textField{if(textField==nameField){[self.dateField resignFirstResponder];}否则if(textField==dateField){[self.nameField resignFirstResponder];}确定只为touch方法中的两个textField辞职…(void)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{[dateField resignFirstResponder];[nameField resignFirstResponder];}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[dateField resignFirstResponder];    
[nameField resignFirstResponder];
}