Dismiss The Keyboard - Multiple UITextFields in iOS 7

前端 未结 8 1143
盖世英雄少女心
盖世英雄少女心 2021-01-31 22:56

below you\'ll find my .h & .m files for my primary viewcontroller.

I have 3 questions.

1.) Because I have multiple uitextfields, do I have to set each with

相关标签:
8条回答
  • 2021-01-31 23:10
      -(BOOL) textFieldShouldReturn: (UITextField *) textField{
    
            [textField resignFirstResponder];
            return YES;
      }
    

    also connect your UITextField Delegate.

    0 讨论(0)
  • 2021-01-31 23:11

    This answer works for iOS 7 and arc,

    1. dismiss keyboard when user touches return: in ViewController add the following action

      -(IBAction)textFieldReturn:(id)sender
      {
          [sender resignFirstResponder];
      }
      

    next, in main.storyboard select the textField and from the connections inspector control + drag "Did End On Exit" event to the view controller.

    1. dismiss keyboard when user touches background: implement the following method in the ViewController

       - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
      
              UITouch *touch = [[event allTouches] anyObject];
              if ([YOUR_TEXT_FIELD isFirstResponder] && [touch view] != YOUR_TEXT_FIELD) {
                  [YOUR_TEXT_FIELD resignFirstResponder];
              }
              [super touchesBegan:touches withEvent:event];
          }
      
    0 讨论(0)
  • 2021-01-31 23:11

    When the UITextField in question calls the delegate method of - (BOOL)textFieldShouldReturn:(UITextField*)textField it passes itself in as the argument.

    So the specific textField available as an argument to this method IS the specific one you care about. Within this delegate method, you can just refer to it as "textField".

    That means that you should use what Mirko Catalano advised calling resignFirstResponder on textField rather than on the individual properties like you were doing.

    Mirko's suggestion to verify that the delegate is indeed assigned is critical as well. You'll want to make sure that ALL of your UITextFields in the nib or storyboard have their delegate property pointing to File's Owner. Otherwise the delegate message will go nowhere and promptly be ignored!

    0 讨论(0)
  • 2021-01-31 23:17

    Here's what I use in my code. It works great and is more efficient than the other answers.

    In yourviewcontroller.h add:

    @property (nonatomic) UITapGestureRecognizer *tapRecognizer;

    Now in the .m file, add this to your ViewDidLoad function:

    - (void)viewDidLoad {
        //Keyboard stuff
        tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didTapAnywhere:)];
        tapRecognizer.cancelsTouchesInView = NO;
        [self.view addGestureRecognizer:tapRecognizer];
    }
    

    Also, add this function in the .m file:

    - (void)handleSingleTap:(UITapGestureRecognizer *) sender
    {
        [self.view endEditing:YES];
    }
    
    0 讨论(0)
  • 2021-01-31 23:18

    In swift 3 :

     //Dismiss keyboard , When touch outside
     override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?)
        {
            self.view.endEditing(true)
        }
    
    0 讨论(0)
  • 2021-01-31 23:22

    Resigning the textField: All your textField.delegate should be set as ViewController's object. And then implement the below delegate method.

    -(BOOL) textFieldShouldReturn: (UITextField *) textField {
    [textField resignFirstResponder];
        return YES;
    }
    

    To dismiss Keyboard on tap of the View: Add a Tap gesture to your ViewController.view as follows:

    //declare a property to store your current responder
    @property (nonatomic, assign) id currentResponder;
    
    
    //in viewDidLoad:
    
    UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignOnTap:)];
        [singleTap setNumberOfTapsRequired:1];
        [singleTap setNumberOfTouchesRequired:1];
        [self.view addGestureRecognizer:singleTap];
        [singleTap release];
    
    //Implement the below delegate method:
    
    - (void)textFieldDidBeginEditing:(UITextField *)textField {
        self.currentResponder = textField;
    }
    
    //Implement resignOnTap:
    
    - (void)resignOnTap:(id)iSender {
        [self.currentResponder resignFirstResponder];
    }
    // was missing ; after the call --> [self.currentResponder resignFirstResponder]
        // also in textFieldDidEndEditing set self.currentResponder = nil;
    
    0 讨论(0)
提交回复
热议问题