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?