Using `textField:shouldChangeCharactersInRange:`, how do I get the text including the current typed character?

前端 未结 10 2034
粉色の甜心
粉色の甜心 2020-12-02 05:23

I\'m using the code below to try and have textField2\'s text content get updated to match textField1\'s whenever the user types in textField1

10条回答
  •  不知归路
    2020-12-02 06:07

    Swift version for it :

    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
    
        if string == " " {
            return false
        }
    
        let userEnteredString = textField.text
    
        var newString = (userEnteredString! as NSString).stringByReplacingCharactersInRange(range, withString: string) as NSString
    
        print(newString)
    
        return true
    }
    

提交回复
热议问题