0

I have a simple application where the user inputs information into a series of UITextFields and a few other components in order for the app to populate with information.

I have 4 UITextFields (Name, Event, Subevent and Amount), 1 UITextView (notes), a UIDatePicker and a UISegmentedControl (Status).

The Name, Event, Amount and Status are required, where the Subevent and Notes are optional.

I've put some logic to disable the "Save" BarButtonItem if the the required fields are not filled in; however I'm noticing my logic is not working correctly.

Here's the logic:

- (void) checkTextFields
{
    if (([self.nameTextField.text length] != 0 && [self.itemTextField.text length] != 0 && [self.occasionTextField.text length] != 0 && [self.isReceivedSegment selectedSegmentIndex] == 0) || [self.isReceivedSegment selectedSegmentIndex] == 1)
    {
        NSLog(@"This shouldn't be run if not every field is filled in");
        self.saveButton.enabled = TRUE;
    }
    else
    {
        NSLog(@"Run");
        self.saveButton.enabled = FALSE;
    }
}

I'm calling this from the viewDidLoad:

self.saveButton.enabled = FALSE;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkTextFields) name:UITextFieldTextDidChangeNotification object:nil];

[self.isReceivedSegment addTarget:self action:@selector(checkTextFields) forControlEvents:UIControlEventValueChanged];

Problem

The problem is: if I enter the Name, Event and Select a Segment in the SegmentedControl, it SHOULD NOT enable the Save button because the Amount is not filled in.

The same applies if I put in the Name, Amount and Select a Segment, the Save button should not be filled in because the Event is not filled in, yet, it is in both of those situations.

Am I missing something obvious?

rmaddy
  • 298,130
  • 40
  • 468
  • 517
amitsbajaj
  • 1,244
  • 1
  • 21
  • 57

1 Answers1

1

Try this:

- (void) checkTextFields
{
    if ([self.nameTextField.text length] != 0 && [self.itemTextField.text length] != 0 && [self.occasionTextField.text length] != 0 && ([self.isReceivedSegment selectedSegmentIndex] == 0 || [self.isReceivedSegment selectedSegmentIndex] == 1))
    {
        NSLog(@"This shouldn't be run if not every field is filled in");
        self.saveButton.enabled = TRUE;
    }
    else
    {
        NSLog(@"Run");
        self.saveButton.enabled = FALSE;
    }
}

The way you have implemented your conditions, with the parentheses, it will evaluate true when your selectedSegmentIndex = 1, regardless of the other conditions. Moving around the parentheses like so should work.

Notice that I have moved the parentheses to surround the OR portion of your conditions.

EDIT add these lines to your view did load method to have the form be checked after editing any text field.

[nameTextField addTarget:self action:@selector(checkTextFields) forControlEvents:UIControlEventEditingChanged];

[itemTextField addTarget:self action:@selector(checkTextFields) forControlEvents:UIControlEventEditingChanged];

[occasionTextField addTarget:self action:@selector(checkTextFields) forControlEvents:UIControlEventEditingChanged];
William Riley
  • 878
  • 7
  • 13
  • Dear @William - That's so interesting with the parenthesis! Thanks so much for that - that's working in the first situation. So if you add everything except the "Amount" text field (itemTextField), then it shows the Save only when you add that in. However, if you add a new entry, add everything in and add the occasionTextField last, it doesn't enable the save button.. – amitsbajaj Apr 15 '14 at 14:30
  • @Lavanya That is probably because the checkTextFields() method is called only when you change the selectedSegmentIndex, right? It is not a good place to validate the text fields, for exactly that reason. It will only work if the last thing the user does is select a segment index. You get what I am saying? – William Riley Apr 15 '14 at 14:34
  • ahhhh.. that makes complete sense actually and I tested it out and it did exactly that, only works if the last thing you do is check the segment. Saying that, the same situation with the "itemTextField" being the last entered textField works with the save button, but not with the event.. minor, but I'll try to work on this – amitsbajaj Apr 15 '14 at 14:37
  • Sure. If you need any more help, let me know. And if this answer solved your original question, please feel free to accept it by clicking the check mark next to it :). Happy coding. I can give you some more guidance on how to accomplish your end-game if you would like to chat. – William Riley Apr 15 '14 at 14:39
  • Thanks very much William - this has been a really great help! I really appreciate it and I would love a chat actually because there's another similar situation that I'm kinda stumped on (relatively new to programming :)) – amitsbajaj Apr 15 '14 at 14:41
  • Have a look at my answer again. I am not able to test them, but those lines should make the form be checked when editing any of the three required fields. – William Riley Apr 15 '14 at 14:43
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50700/discussion-between-lavanya-and-william-riley) – amitsbajaj Apr 15 '14 at 14:45