我尝试根据文本的长度更改我的按钮状态,用户放入文本字段。我不知道为什么跟随逻辑不起作用:
-(IBAction)doneButtonFired:(id)sender{
if ([self.myTextField.text length] > 0){
self.doneButton.enabled = YES;
NSLog(@"YES");
[self.delegate stringChangedTo:self.myTextField.text atIndex:self.indexToPass];
[self.delegate completeTask:[self getTimeDifference:self.oldDate]];
[self.navigationController popViewControllerAnimated:YES];
} else if ([self.myTextField.text length] < 1){
NSLog(@"NOO");
self.doneButton.enabled = NO;
}
}
这也不起作用(因为它检查旧值):
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
if ([self.myTextField.text length] > 0){
NSLog(@" > 0");
} else if ([self.myTextField.text length] == 0){
NSLog(@" < 0");
}
return YES;
}
如何解决这个简单的问题?
答案 0 :(得分:2)
尝试放置类似的内容:
[self.myTextField addTarget:self
action:@selector(textFieldDidChange:)
forControlEvents:UIControlEventEditingChanged];
在viewDidLoad中:
-(void) textFieldDidChange:(UITextField *)textField
{
if([textField.text length]>0){
self.doneButton.enabled = YES;
}else{
self.doneButton.enabled = NO;
}
}