문제

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?

도움이 되었습니까?

해결책

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];
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top