简体   繁体   中英

becomeFirstResponder() is not bringing up the keyboard - Swift 5 - iOS 14

I am really fresh in Swift coding. Hoping someone knows what is going wrong here. In the code below, I am trying to bring up the keyboard when its loaded - Seems to be working on iPhone SE Simulator, however its not working in iPhone 11 Simulator. Please let me know if more details are needed.

import UIKit

class ViewController: UIViewController {
    
    @IBOutlet weak var textField: UITextField!
    
    @IBOutlet weak var labelPasteboard: UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        textField.becomeFirstResponder()
        
    }

    @IBAction func trashPressed(_ sender: Any) {
        textField.text = ""
    }
    @IBAction func buttonPressed(_ sender: Any) {
        labelPasteboard.text = "Hello \(textField.text!)"
    
    }
    
}

in your viewDidLoad the controller does don't know about your textField

try add

textField.becomeFirstResponder()

in viewWillAppear or viewDidAppear methods

You need to ensure that in the simulator menu under I/O > Keyboard, make sure that Connect Hardware Keyboard is unticked. Sometimes it will automatically default to using your machine keyboard instead of using the software keyboard.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM