20

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?

Mark
  • 6,108
  • 3
  • 34
  • 49
Tejas Bharambe
  • 672
  • 1
  • 12
  • 27

8 Answers8

49

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.

thavasidurai
  • 1,972
  • 1
  • 26
  • 51
18

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
)
Jacob Arvidsson
  • 618
  • 7
  • 13
3

I think you missed the addsubview the txtAddNew to the Mainview

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

   // Open keyboard
  [txtAddNew becomeFirstResponder];
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
1

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

[txtAddNew performSelectorOnMainThread:@selector(becomeFirstResponder) withObject:nil afterDelay:0.0];
Sport
  • 8,570
  • 6
  • 46
  • 65
1

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

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)
Antoine
  • 23,526
  • 11
  • 88
  • 94
0

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.

Zoltan Varadi
  • 2,468
  • 2
  • 34
  • 51
0

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.

apolo
  • 441
  • 4
  • 18