Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resignFirstResponder keyboard will NOT go away before segue

I am stumped and I hope someone can help.

I am calling the resign first responder method for all five of my text fields prior to a segue. The segue occurs, if the keyboard was visible prior to the segue, the keyboard remains no matter what I do. This did not happen in IOS6. It is only happening in IOS7.

Thank you so much in advance for your assistance.

Here is the scenario:

The user touches one text field at time to enter data. The keyboard has no problems changing from first responder from one field to the next and can be resigned from the DONE button without issues. The problem comes when the user touches a field that will be populated from the picker view. If the keyboard was visible from one of the previous text fields, it won't go away.

I have this code attempting to resignFirstResponder on the editingDidBegin action of two of the fields. I am using these two fields to hold numbers but I am filling them from a picker on the next view.

- (IBAction)txtRatioOrHullTypeTouched:(id)sender 
{    
    // Hide the keyboard before the segue to the picker occurs.
    [self.txtPitch resignFirstResponder];
    [self.txtRPM resignFirstResponder];
    [self.txtSlipOrSpeed resignFirstResponder];
    [self.txtRatio resignFirstResponder];
    [self.txtHullType resignFirstResponder];

    segueToPicker = YES; // Raise flag indicating that this segue is to the picker.
    [self performSegueWithIdentifier:@"toPicker" sender:sender];
}

I also put this same code in the viewWillDisappear as shown here:

- (void)viewWillDisappear:(BOOL)animated // Unchanged
{
    // Hide the keyboard before the segue to the picker occurs.
    [self.txtPitch resignFirstResponder];
    [self.txtRPM resignFirstResponder];
    [self.txtSlipOrSpeed resignFirstResponder];
    [self.txtRatio resignFirstResponder];
    [self.txtHullType resignFirstResponder];

    [super viewWillDisappear:animated];
}

Both of these methods are on the initial view, ViewController.m file.

like image 310
Larry J Avatar asked Mar 22 '23 22:03

Larry J


2 Answers

I ended up here removing the text field causing the problem and replacing them with buttons. No scenario I tried (dozens) got this code to work as expected in IOS7, even though it all worked flawlessly in IOS6.

like image 152
Larry J Avatar answered Apr 16 '23 00:04

Larry J


I tried all of the above and it worked as long as i dismissed the controller with a button. The function that was called when pressing the button could call the TextField's resignFirstResponder() function and all was well.

However, when an edge swipe was performed to dismiss the controller the keyboard kept popping up the next time I showed it. In my code I reuse the same controller between views. This might not be wise but, it's snappy!

After trying everything the internet had written (well not really, but pretty close) about this I found that i could implement the TextField's textViewShouldBeginEditing() and return false between the ViewControllers ViewDidDisappear and ViewDidAppear. It's ha hack, but it did the trick when nothing else worked.

I hope this helps you guys!

Swift code:

In my ViewController

override func viewDidAppear(animated: Bool) {
  super.viewDidAppear(animated)
  myTextField.allowEdit = true
}

override func viewDidDisappear(animated: Bool) {
  super.viewDidDisappear(animated)
  myTextField.allowEdit = false
}

In my TextField class

class MyTextField: UIView, UITextFieldDelegate {
  var allowEdit = true

  func textFieldShouldBeginEditing(textView: UITextView) -> Bool {
    return allowEdit
  }
}
like image 25
MikaelHalen Avatar answered Apr 16 '23 00:04

MikaelHalen