Dismiss The Keyboard - Multiple UITextFields in iOS 7

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

提交回复
热议问题