I have a UIViewController that have 2 segmented controls which are Takeaway
and Delivery
, and 5 UITextFields which are adressField
, stateField
, cityField
, postcodeField
and phoneField
.
I would like to disable user input for each UITextfield
except phoneField
whenever user choose Takeaway
,and enable all the UITextfield
whenever user choose Delivery
.
My problem is that the phoneField
doesn't work even though when I logged the value, phoneField.isEnabled
return YES
. And all the fields work when I choose Delivery
segment. What do I miss here?
Also how do I set the segment Takeaway
as the default state when user enter the view? since currently I need to select the SegmentedControl
for changes to take effect.
Here's my IBAction
snippet for the problem.
- (IBAction)segmentChanged:(id)sender
{
NSLog(@"SEGMENT: %d", [segment selectedSegmentIndex]);
switch ([segment selectedSegmentIndex]) {
case 0:
{
self.navigationItem.title = @"Takeaway";
addressField.enabled = N0;
...
// Enable phoneField here.
phoneField.enabled = YES;
}
break;
case 1:
{
self.navigationItem.title = @"Delivery";
...
}
break;
default:
break;
}
// code to change the opacity of the textfield.
[addressField setAlpha:[segment selectedSegmentIndex]==1 ? 1.0f : 0.3f];
[addressLabel setAlpha:[segment selectedSegmentIndex]==1 ? 1.0f : 0.3f];
[addressBox setUserInteractionEnabled:[segment selectedSegmentIndex]==1];
}