D. Finna
D. Finna

Reputation: 467

Select textfield and bring up keyboard before showing viewcontroller - swift

I have the following code that is sending information to the next UIViewController. In the next UIViewController I have a UITextField. When the function commentsTapped is being used (which means it will go to the next UIViewController), it should automatically "select" the UITextField and bring up the keyboard.

Here is my code:

func commentsTapped(cell: updateTableViewCell) {
  let allDataSend = cell.pathDB
  self.performSegueWithIdentifier("showComments", sender: allDataSend)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
  if segue.identifier == "showComments" {
    if let nextVC = segue.destinationViewController as? commentsTableViewController {
      nextVC.dataGotten = sender! as! String
    }
  }
}

Does anyone know how to do that?

Upvotes: 2

Views: 2134

Answers (2)

Bhavin Ramani
Bhavin Ramani

Reputation: 3219

In your commentsTableViewController's viewDidLoad method add this:

override func viewDidLoad() {
  super.viewDidLoad()
  // Do any additional setup after loading the view.

  yourTextfield.becomeFirstResponder()
}

Upvotes: 1

Pratik Mistry
Pratik Mistry

Reputation: 2945

This way, you will see the keyboard appear after the view controller is loaded:

override func viewDidAppear(animated: Bool) {
  super.viewDidAppear(animated)
  self.textField.becomeFirstResponder()
}

Upvotes: 2

Related Questions