Enabling done button after inserting one char in a textfield: textFieldDidEndEditing: or textFieldShouldBeginEditing: or?

后端 未结 7 1688
迷失自我
迷失自我 2020-12-28 08:43

I would like to enable the done button on the navbar (in a modal view) when the user writes at least a char in a uitextfield. I tried:

  • textFieldDidEndEditing:
7条回答
  •  南方客
    南方客 (楼主)
    2020-12-28 09:32

    try and go with:

    -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    int lenght = editingTextField.text.length - range.length + string.length;
    if (lenght > 0) {
        yourButton.enabled = YES;
    } else { 
        yourButton.enabled = NO;
    }
    return YES;
    

    }

    This answer was marked correct when infact a better solution existed by 'w4nderlust' below. This answer is theirs, let them take the credit!

提交回复
热议问题