0

I would like the keyboard to be shown without the user touch the username text field.

I found the code from How to open the keyboard automatically on UITextField?

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

So I tried this

@IBOutlet var userNameTF: UITextField!

@IBOutlet var passwordTF: UITextField!


override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(animated)

    userNameTF.becomeFirstResponder()

}

but it gives an error

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

Sugarman
  • 13
  • 3
  • Why are you declaring `IBOutlets`? You should not - unless you are (1) defining these `UITextFields` in IB, (2) actually wiring them up, and most of all (3) **not** force-unwrapping them. My best ideas? Learn a few things - including what this error means obviously (hint: we see several variations of this error in a question every week). Also, what makes for a good question (*are* you using IB?). Finally, maybe how to raise the keyboard *though* code. –  Nov 06 '17 at 02:24
  • Did you connect your IBOutlet via storyboard? – Maor Nov 06 '17 at 04:39

2 Answers2

0

Double check your outlets are connected if you are using the storyboard, and then try this:

1) Place your userNameTF.becomeFirstResponder in viewDidLoad() instead of viewDidAppear().

2) Convert

@IBOutlet var userNameTF: UITextField!
@IBOutlet var passwordTF: UITextField!

to:

@IBOutlet weak var userNameTF: UITextField!
@IBOutlet weak var passwordTF: UITextField!
Louis Leung
  • 508
  • 4
  • 11
  • Thanks for reply. I tried your method and it gives the same error – Sugarman Nov 06 '17 at 03:42
  • @Sugarman this works in my XCode. Please verify you deleted userNameTF.becomeFirstResponder from viewDidAppear() when you moved to viewDidLoad(). If this doesn't work, let's check if this error is not generated by something else that you have not shared with us. Do you still get the error when you comment out userNameTF.becomeFirstResponder in viewDidLoad()? – Louis Leung Nov 06 '17 at 04:46
0

I think problem is with IBOutlets

@IBOutlet var userNameTF: UITextField!

1) You are using a strong reference here which will cause you issue later that even after the presented View with these TF is removed memory will not be released as reference to property are strong

2) Try using @IBOutlet weak var usernameTF: UITextField! and when you make changes remove the connected outlet from storyBoard and reconnect it with Written outlet and build

Crash - Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

occurs in two case -

1) This is occurring as you are trying to access a property which is not properly connected 

2) When you are Presenting the same presented view In hierarchy 

Try using Solutions mentioned and let me know if issue still exists

import UIKit

class TFVC: UIViewController,UITextFieldDelegate {

    @IBOutlet weak var usernameTF: UITextField!
    @IBOutlet weak var passowrdTF: UITextField!

    override func viewDidLoad() {
        super.viewDidLoad()

        usernameTF.delegate = self
        passowrdTF.delegate = self

    }

    override func viewDidAppear(_ animated: Bool) {
        usernameTF.becomeFirstResponder()
    }
iOS Geek
  • 4,825
  • 1
  • 9
  • 30