becomeFirstResponder not working in iOS 8

2020-02-03 12:36发布

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?

8条回答
Ridiculous、
2楼-- · 2020-02-03 12:56

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.

查看更多
成全新的幸福
3楼-- · 2020-02-03 12:57

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
)
查看更多
再贱就再见
4楼-- · 2020-02-03 12:57

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.

查看更多
太酷不给撩
5楼-- · 2020-02-03 13:03

I think you missed the addsubview the txtAddNew to the Mainview

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

   // Open keyboard
  [txtAddNew becomeFirstResponder];
查看更多
Animai°情兽
6楼-- · 2020-02-03 13:04

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

[txtAddNew performSelectorOnMainThread:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
查看更多
Animai°情兽
7楼-- · 2020-02-03 13:09

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.

查看更多
登录 后发表回答