简体   繁体   中英

Different UITextField behavior on phone vs simulator

I have a SKLabelNode that I'm updating using an invisible UITextField. So when the user types something in the keyboard, the label changes to that string. The code below is what I'm using. It works perfect in the iOS simulator, however when I run it on my phone, the UITextField is empty each time I type a character, so the string never gets longer. What am I missing?

    var inputTextField = UITextField(frame: CGRectMake(0, 0, 100, 30))
    var inputTextLabel = SKLabelNode(fontNamed:"Thonburi")


override func didMoveToView(view: SKView) {
    inputTextLabel.text = "";
    inputTextLabel.fontSize = 25;
    inputTextLabel.position = CGPoint(x:10, y:CGRectGetMidY(self.frame));
    inputTextLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentMode.Left
    self.addChild(inputTextLabel)

    inputTextField.delegate = self
    inputTextField.hidden = true
    inputTextField.becomeFirstResponder()
    self.inputTextField.keyboardType = UIKeyboardType.ASCIICapable
    self.view?.addSubview(inputTextField)

}

func textField (textField: UITextField, shouldChangeCharactersInRange range:NSRange, replacementString string: NSString){
    var newString = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
    inputTextLabel.text = newString
}

I figured it out, the textField function is supposed to return a bool like so

    func textField (textField: UITextField, shouldChangeCharactersInRange range:NSRange, replacementString string: NSString) -> Bool {
    var newString = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)
    inputTextLabel.text = newString
    return true
}

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