Enabling button if I write in TextField

大兔子大兔子 提交于 2019-12-12 13:18:47

问题


I am having some problems with enabling a button when you write something in a TextField. I got it to disable if a there's nothing in the TextField but I can't make it to enable again when you write something in it.

I have something like this:

- (void)viewDidLoad {
    [super viewDidLoad];

    NSUInteger textLength = [_Name.text length];
    [_doneButton setEnabled:(textLength > 0)];

}

回答1:


Try this

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if([textField.text length]>0){
    [doneButton setEnabled:YES];        

}
else{
 [doneButton setEnabled:NO ;
}

Hope this May be help you..




回答2:


Set delegate to the UITextField and create a method for text change in ViewDidLoad as follows.

[self.textFieldTemp addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

And add the method as follows.

- (void)textFieldDidChange:(UITextField *)textField {
    if(textField == self.textFieldTemp) 
        self.buttonTemp.enabled = ([self.textFieldTemp.text length] > 0) ? YES : NO; }



回答3:


Use UITextField delegate method to enable your button again. When your textField characters are changed, delegate method will be called, enable your button here.

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
     _doneButton.enabled = YES;
}



回答4:


Put the lines in textfield delegate method:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

It'll work then. This method is called every time you input a character in textfield. But ViewDidLoad is called only when the viewcontroller is loaded.

Hope it will work for you.




回答5:


These two functions will enable and disable your button according the text in your textfield

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
     _doneButton.enabled = YES;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
if([textField.text length]>0){
   [doneButton setEnabled:YES];        
}
else{
 [doneButton setEnabled:NO ;
}



回答6:


You can use delegates of UITextField to enable and disable the button like this.

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



回答7:


The method I used didn't use the delegates at all. I created this method in the controller class:

- (IBAction)textFieldChanged:(id)sender
{
    self.doneButton.enabled = ([self.Name.text length] > 0) ? YES : NO;
}

Then, in the Interface Builder, I set the text field "Editing Changed" action to target the new method. Then, similar to @Paramasivan Samuttiram, I added an extra call to it in the viewDidLoad:.

[self textFieldChanged:self.Name];

That part allows the button to be in the correct state initially.



来源:https://stackoverflow.com/questions/13966043/enabling-button-if-i-write-in-textfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!