5

hi im a beginner to programming and have been stuck on this task for ages and seem to be getting nowhere.

basically i have several textfields that generates the input information on a different page when the user presses a button. i would like the button to be disabled until all text fields are filled with information.

so far i have this:

    - (void)textFieldDidEndEditing:(UITextField *)textField
{
    // make sure all fields are have something in them

if ((textfbookauthor.text.length  > 0)&& (textfbookedition.text.length > 0)&& (textfbookplace.text.length > 0)&& (textfbookpublisher.text.length > 0) && (textfbookpublisher.text.length > 0) && (textfbooktitle.text.length > 0)  && (textfbookyear.text.length > 0)) {
        self.submitButton.enabled = YES;
    }
    else {
        self.submitButton.enabled = NO;
    }
}

the problem is the 'submitButton' is coming up with an error, what needs to go in its place? i tried to put my button 'bookbutton'in it instead but its not working.

this is my function for the 'generate' button

    -(IBAction)bookbutton:(id)sender;
{
    NSString* combinedString = [NSString stringWithFormat:
                                @"%@ %@ %@.%@.%@:%@.", 
                                textfbookauthor.text,
                                textfbookyear.text,
                                textfbooktitle.text,
                                textfbookedition.text,
                                textfbookplace.text,
                                textfbookpublisher.text];
    BookGenerate*bookg = [[BookGenerate alloc] init];
    bookg.message = combinedString;
    bookg.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:bookg animated:YES];
    [BookGenerate release];
}

if anybody knows how i can make it work or what i need to add please help.

thanks in advance

Ayub
  • 110
  • 1
  • 1
  • 6

5 Answers5

25

Make an Outlet for every UITextField and create an IBAction in your .h:

IBOutlet UITextField *textField1;
IBOutlet UITextField *textField2;
IBOutlet UITextField *textField3;
IBOutlet UIButton *button

- (IBAction)editingChanged;

Connect all the outlets and connect the IBAction to every textfield with editingChanged:

 - (IBAction)editingChanged {
    if ([textfield1.text length] != 0 && [textfield2.text length] != 0 && [textfield3.text length] != 0) {
         [button setEnabled:YES];
     }
     else {
         [button setEnabled:NO];
     }
 }

Note that you can also use [textfield.text isEqualToString:@""] and put a ! in front of it (!means 'not') to recognize the empty textField, and say 'if the textField is empty do...'

And:

- (void)viewDidLoad {
    [super viewDidLoad];
    [button setEnabled:NO];
}
JonasG
  • 9,274
  • 12
  • 59
  • 88
6

If you have a button and you want to enable it only if there is a text in a textfield or textview, implement the follow method

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{    
    if (textView.text.length > 1 || (text.length > 0 && ![text isEqualToString:@""]))
    {
        self.button.enabled = YES;
    }
    else
    {
        self.button.enabled = NO;
    }

    return YES;
}
Raphael Oliveira
  • 7,751
  • 5
  • 47
  • 55
2

Few solutions are available on these posts as well

Key here is using (extending) UITextFieldDelegate class and it's associated function

//Need to have the ViewController extend UITextFieldDelegate for using this feature
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {

    // Find out what the text field will be after adding the current edit
    let text = (textField.text as NSString).stringByReplacingCharactersInRange(range, withString: string)

    if !text.isEmpty{//Checking if the input field is not empty
        button.userInteractionEnabled = true //Enabling the button
    } else {
        button.userInteractionEnabled = false //Disabling the button
    }

    // Return true so the text field will be changed
    return true
}
Community
  • 1
  • 1
Abhijeet
  • 8,561
  • 5
  • 70
  • 76
1

You should use" textfield should change chracters in range" (see apple docs for uitextviewdelegate). because it updates as users type information." Textfield did end editing" only fires when the users finishes editing a textfield or hits enter. In most cases, someone will fill in the last field and leave the cursor sitting there and expect the button to become enabled...but that wont happen using "textfield did end editing" just because they entered input.

You can also do this to check the length

if([self.textfiele.text isEqualToString:@""])
Delete
  • 922
  • 8
  • 12
  • if you have time could you give me an example please? ive been trying to read everywhere and tried many different examples but cant seem to get it to work – Ayub Aug 15 '11 at 15:23
1

In addition to having IBOutlets for each UITextField, it might be helpful to have an IBOutletCollection declared that contains all 3 text fields in an array.

@property (nonatomic, retain) IBOutletCollection(UITextField) NSArray *textFields;

Then in your implementation just @synthesize textFields and you can quickly loop the text fields it contains, checking for text.

- (IBAction)editingChanged
{
    BOOL buttonShouldBeEnabled = YES;

    for (UITextField *field in self.textFields)
        if (!field.text.length)
            buttonShouldBeEnabled = NO;

    button.enabled = buttonShouldBeEnabled;
}
Mark Adams
  • 30,776
  • 11
  • 77
  • 77
  • what is the benefit of this compared to Maxners answer? – Ayub Aug 16 '11 at 12:43
  • by the way do you know how i can generate 2 of the textfields on the other xib when i click the generate button? my first post shows how i generate all the textfields on the other xib but say i wanted to output just textfbookplace n textfbookyear right above this do u know how i could do it? i tried creating another nsstring with format and it doesnt seem to work. – Ayub Aug 16 '11 at 12:50