becomeFirstResponder not working in iOS 8

2020-02-03 12:30发布

问题:

I am using UITextField's method becomeFirstResponder to show the keyboard. This is working in iOS 7. But in iOS 8 this method doesn't show the keyboard.

 UITextField *txtAddNew = [[UITextField alloc] initWithFrame:CGRectMake(10,10,240, 21)];
 txtAddNew.font = [UIFont fontWithName:@"Helvetica" size:16];
 txtAddNew.clearButtonMode = UITextFieldViewModeWhileEditing;
 txtAddNew.returnKeyType = UIReturnKeyDone;
 txtAddNew.delegate = self;
 txtAddNew.autocorrectionType = UITextAutocorrectionTypeNo;
 txtAddNew.tag = 15;

 // Open keyboard
 [txtAddNew becomeFirstResponder];

Is there any way to do it in iOS 8?

回答1:

Try calling becomeFirstResponder like below

[txtAddNew performSelector:@selector(becomeFirstResponder) withObject:nil afterDelay:0];

As per Apple,

A responder object only becomes the first responder if the current responder can resign first-responder status (canResignFirstResponder) and the new responder can become first responder.

You may call this method to make a responder object such as a view the first responder. However, you should only call it on that view if it is part of a view hierarchy. If the view’s window property holds a UIWindow object, it has been installed in a view hierarchy; if it returns nil, the view is detached from any hierarchy.



回答2:

Swift 3

Here is the swift 3 version of the accepted answer. For me to get it to work I also had to add a delay.

txtAddNew.perform(
    #selector(becomeFirstResponder), 
    with: nil, 
    afterDelay: 0.1
)


回答3:

I think you missed the addsubview the txtAddNew to the Mainview

   txtAddNew.tag = 15;
   [self.view addsubview:txtAddNew];  

   // Open keyboard
  [txtAddNew becomeFirstResponder];


回答4:

Sometimes it helps to first resign the first responder:

var tapGesture = UITapGestureRecognizer()
tapGesture.rac_gestureSignal().subscribeNext { [weak self] (_) -> Void in
    self?.inputTextField.resignFirstResponder()
    self?.inputTextField.becomeFirstResponder()
}

self.charViewsContainer.addGestureRecognizer(tapGesture)


回答5:

Swift 4 and iOS 11

In my case, no mater what I tried, I was not able to set first responder until I tried this.

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    if cell.isKind(of: YOURTABLEVIEWCELL.self) {
        if let yourCell = cell as? YOURTABLEVIEWCELL{
            yourCell.yourUiTextField.becomeFirstResponder()
        }
    }
}

Hope this helps someone stuck with the same problem.



回答6:

add this line in your code . i hope it will work

[txtAddNew performSelectorOnMainThread:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0];


回答7:

For iOS 9.3, the following worked for me:

 private var searchTextFieldShouldReturn = false

    override func viewWillAppear(animated: Bool) {
            super.viewWillAppear(animated)

            self.searchTextFieldShouldReturn = false
            self.searchTextField.becomeFirstResponder()
        }
    override func viewWillDisappear(animated: Bool) {
        super.viewWillDisappear(animated)

        self.searchTextFieldShouldReturn = true
        self.searchTextField.resignFirstResponder()
    }

    func textFieldShouldReturn(textField: UITextField) -> Bool {
            self.searchTextFieldShouldReturn = true
            textField.resignFirstResponder()

            return true
        }

    func textFieldShouldEndEditing(textField: UITextField) -> Bool {

            return self.searchTextFieldShouldReturn
        }

So You need to make sure You also implement the textFieldShouldEndEditing delegate.



回答8:

Just in case others have the same issue as me:

I had an IBOutlet connected to my UITextField. Erroneously I was initializing this in my viewDidLoad method. That is, [[textField alloc] init];. I removed this and everything worked again.