UITextField shouldChangeCharactersInRange方法无法按预期工作

时间:2015-07-05 03:58:26

标签: ios xcode swift uitextview uitextviewdelegate

func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

   //step1: Figure out what the new text will be, if we return true
   var newText: NSString = textField.text
   println("test")

   //step2. 
   newText = newText.stringByReplacingCharactersInRange(range, withString: string)

    //step3: hide the label if the newText will be an empty string
    self.characterCountLabel.hidden = (newText.length == 0)

    //step4: Write the length of newText into the label
    self.characterCountLabel.text = String(newText.length)

    //step: returning true gives the text field permission to change its text
    return true;
}

现在,当我尝试修改文本字段时,应调用此函数。当我尝试使用断点调试此代码时,首先执行step1,然后立即执行步骤4。在执行之后返回到步骤2,然后是步骤3,然后是步骤5,最后文本字段被修改。执行不应该从第1步到第5步顺序执行吗?

0 个答案:

没有答案