becomeFirstResponder not working in iOS 8

前端 未结 8 766
不知归路
不知归路 2020-12-09 03:55

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.

 U         


        
相关标签:
8条回答
  • 2020-12-09 04:17

    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.

    0 讨论(0)
  • 2020-12-09 04:18

    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.

    0 讨论(0)
  • 2020-12-09 04:18

    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.

    0 讨论(0)
  • 2020-12-09 04:19

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

    [txtAddNew performSelectorOnMainThread:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
    
    0 讨论(0)
  • 2020-12-09 04:30

    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.

    0 讨论(0)
  • 2020-12-09 04:30

    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)
    
    0 讨论(0)
提交回复
热议问题