-1

I created a text field in iOS and I want the keyboard to pop out every time I open it. I tried the following code in .swift file:

@IBOutlet weak var messageTextview: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()
    self.messageTextview.becomeFirstResponder()
}

However, the keyboard didn't show. Can someone please help me with it?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
ZHEN BIAN
  • 13
  • 1
  • 6
  • Since `viewDidLoad` is called before the view is presented on the screen, it's likely that the OS has simply, ignored your request, as it's not possible for the field to become the first responder. Instead, maybe use `viewDidAppear` instead – MadProgrammer Oct 02 '18 at 21:54
  • Are you using a simulator or device? – Ashik Oct 02 '18 at 22:23
  • I'm using a simulator – ZHEN BIAN Oct 02 '18 at 22:59
  • @MadProgrammer Sorry, I don't quite understand want you mean, do you mean I should make a new function called viewDidAppear, and put self.messageTextview.becomeFirstResponder() inside it? Thank you! – ZHEN BIAN Oct 02 '18 at 23:10
  • @ZHENBIAN Yes. Perhaps you should have a look at [Understand the View Controller Lifecycle](https://developer.apple.com/library/archive/referencelibrary/GettingStarted/DevelopiOSAppsSwift/WorkWithViewControllers.html) and [Looking to understand the iOS UIViewController lifecycle](https://stackoverflow.com/questions/5562938/looking-to-understand-the-ios-uiviewcontroller-lifecycle) to gain a better understand of the lifecycle of a `UIViewController` – MadProgrammer Oct 02 '18 at 23:12
  • @MadProgrammer Thank you! and I add override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.messageTextview.becomeFirstResponder() } – ZHEN BIAN Oct 02 '18 at 23:28

1 Answers1

0

If you are using a simulator and keyboard does not show on that then press ⇧+⌘+K

User18474728
  • 363
  • 2
  • 11