Dismiss The Keyboard - Multiple UITextFields in iOS 7

前端 未结 8 1142
盖世英雄少女心
盖世英雄少女心 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: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];
          }
      

提交回复
热议问题