手机与模拟器上的不同UITextField行为

时间:2015-04-04 23:09:55

标签: ios iphone swift sprite-kit

我有一个SKLabelNode,我使用不可见的UITextField进行更新。因此,当用户在键盘中键入内容时,标签会更改为该字符串。以下代码是我正在使用的代码。它在iOS模拟器中工作得很好,但是当我在手机上运行时,每次输入一个字符时UITextField都是空的,所以字符串永远不会变长。我错过了什么?

    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
}

1 个答案:

答案 0 :(得分:0)

我想通了,textField函数应该像这样返回一个bool

    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
}