0

I have a view controller that controls 2 text fields and an array that displays in a table. How would I go about keeping a button disabled until the 2 fields have at least one character and the array is not empty. I am thinking about using cocoa bindings, however I can't seem to figure out a solution.

Currently my button is binded to

BOOL buttonIsEnabled;

I use that in a notification function in order to keep the button disabled, except the button will only reenable if I call that notification function.

-(void)controlTextDidChange

This means if i make a change in an array, the button wont reenable until I re-enter text. I can't seem to figure out an alternative solution. Any suggestions? Thank you.

4

2 回答 2

0

You can create a custom cell, give outlets to your 2 textfields and button.

Now on cell for row at index path after filling the textfields text values, put a condition checking the length of the textfields. If length is greater than 0 you can enable the button or keep it disable.

Along with this you need to put same code in delegate method (textFieldDidChange) of textfield. So that when ever new text is entered the button gets enable or disabled.

于 2013-09-24T04:32:39.647 回答
0

One solution:

  • bind the two text fields two two strings (say text1 and text2)

  • in the text field bindings check Continuously Updates Value

Add this code:

- (BOOL)buttonIsEnabled {

    return (self.text1.length>0 && self.text2.length>0);
}
+ (NSSet *)keyPathsForValuesAffectingButtonIsEnabled {

    return [NSSet setWithObjects:@"text1",@"text2",nil];
}
  • Lastly bind the buttons enabled binding to buttonIsEnabled.

Because of the Continuously Updates Value text1 or text2 will change whenever characters are added to or removed from the text fields.

And the + (NSSet *)keyPathsForValuesAffectingButtonIsEnabled method will cause a Key-Value change notification to be posted for buttonIsEnabled whenever text1 or text2 change.

于 2013-09-24T04:34:39.823 回答