简体   繁体   English

如果按下返回键,如何停止调用方法

[英]how to stop method from calling if return key is pressed

I have iphone app in which i am calling code one on textFieldShouldReturn and other on didEndEditing . 我有一个iPhone应用程序,其中我在textFieldShouldReturn上调用一个代码,在didEndEditing上调用另一个代码。

I want that when user return press the app should not called didEndEditing code and if user does not press return then call. 我希望当用户返回时按应用程序不应该调用didEndEditing代码,如果用户不按返回就可以调用。

-(void)textFieldDidEndEditing:(UITextField *)textField
{
   if (textField==tagTextField) {
        [self showAnimationBack];
    }
    if (textField.tag==2 && [valueReturn isEqualToString:@"Yes"]) {
       if (textField.text.length > 0 || ![tagTextField.text isEqualToString:@""]) {
           [textField resignFirstResponder];
           [tagArray addObject:tagInputField.text];
           [tableView reloadData];
           tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.contentSize.height);
          [tableView.layer setCornerRadius:7.0f];
           [tableView.layer setMasksToBounds:YES];
           tableView.layer.borderWidth = 0.5;
           tableView.layer.borderColor = [UIColor grayColor].CGColor;
           [self showAnimationBack];
     }
}

 - (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if(textField.tag == 2)
    {
        if (textField.text.length > 0 || ![tagTextField.text isEqualToString:@""]) {
        [textField resignFirstResponder];
        [tagArray addObject:tagInputField.text];
        [tableView reloadData]; 
        tableView.frame = CGRectMake(tableView.frame.origin.x, tableView.frame.origin.y, tableView.frame.size.width, tableView.contentSize.height);
        [tableView.layer setCornerRadius:7.0f];
        [tableView.layer setMasksToBounds:YES];
         tableView.layer.borderWidth = 0.5;
         tableView.layer.borderColor = [UIColor grayColor].CGColor;
         [self showAnimationBack];
     }
}

I call this also when user press return 当用户按下回车键时,我也称其为

- (void)keyboardDidHide:(NSNotification *)aNotification {
       valueReturn=@"Yes";
}

使用isEditing值,因为当您按下返回键时(很可能)从第一响应者处退出了文本视图,因此textFieldDidEndEditing时应使文本字段处于非编辑模式。

I am not sure if this a proper way of doing it but you can try this. 我不确定这样做是否正确,但是您可以尝试一下。 Declare a variable isReturnPressed and make it YES in textFieldShouldReturn: and make it NO in textFieldDidEndEditing: . 声明一个变量isReturnPressed并在textFieldShouldReturn: isReturnPressed其设置为YES ,并在textFieldDidEndEditing:中将其设置为NO So you can check the condition as follows.. 因此,您可以按以下方式检查条件。

-(void)textFieldDidEndEditing:(UITextField *)textField
{
   if (isReturnedPressed == NO) {
      // Do your stuff

   }
   else {
       isReturnedPressed = NO
   }
}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM