Question

I have a view which contains different textfield.

Clicking on each taxtfield brings up a keyboard and if I click outside the textField the keyboard disappears but the view is also disappear! how can I change the code so that i can only get out of the view using cancel button and tapping down the view not clicking outside of the texfield or by clicking on the background.

        - (void)viewDidLoad
    {

    [super viewDidLoad];
         [self.navigationController setNavigationBarHidden:YES];
          self.view.backgroundColor = [self.appDel.styleManager appBackgroundGradientWithFrame:self.view.bounds];
         self.tapView.backgroundColor = [UIColor clearColor];


         UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                              action:@selector(endPageEdit:)];
tap.delegate = self;
        [self.view addGestureRecognizer:tap];


        }



- (void)endPageEdit:(UIGestureRecognizer *)sender
{
        if (sender.view == self.view) {
        [self.view endEditing:YES];
        if ([self isFormVisible]) {
           [self handleTap:sender];
        }
    }

}
Was it helpful?

Solution

In your endPageEdit: method, tell the textFields to resignFirstResponder.

- (void)endPageEdit:(UIGestureRecognizer *)sender {

    // assumes you have an outlet set up for each of the textFields
    [self.textFieldA resignFirstResponder];
    [self.textFieldB resignFirstResponder];

    if (sender.view == self.view) {
        [self.view endEditing:YES];
        if ([self isFormVisible]) {
           [self handleTap:sender];
        }
    }
}

Only zero or one of them will be the first responder, but there's no downside to resigning first responder when the control is not first responder. The reason the view is disappearing must have something to do with the endEditing: method or the handleTap method which you did not post.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top