【问题标题】:Different UITextField behavior on phone vs simulator手机与模拟器上的不同 UITextField 行为
【发布时间】:2015-04-04 23:09:54
【问题描述】:

我有一个使用不可见 UITextField 更新的 SKLabelNode。因此,当用户在键盘上键入内容时,标签会更改为该字符串。下面的代码是我正在使用的。它在 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
}

【问题讨论】:

    标签: ios iphone swift sprite-kit


    【解决方案1】:

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

        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
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多