我有一个iOS应用程序,我有一个UITextField,其最大字符长度我使用委托方法设置:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
但是,我现在意识到,当用户在同一个UITextField中输入至少一个字符时,我还需要启用UIButton *myButton
。我该如何实现呢?
以下是我在委托方法中的相关代码:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
return (newLength > 45) ? NO : YES;
}
答案 0 :(得分:4)
从禁用的UIButton开始,如果textField文本长度大于0,则启用该按钮。
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
// Enable/disable the button depending on the length of the text
if (newLength > 0) button.enabled = YES;
else button.enabled = NO;
return (newLength > 45) ? NO : YES;
}