0

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];
}
Faiz Mokhtar
  • 938
  • 1
  • 10
  • 24

2 Answers2

0

I guess the problem is:

   [addressBox setUserInteractionEnabled:[segment selectedSegmentIndex]==1];

addressBox contains phoneField, right? It means you disabling user interaction in whole view and it's subviews. Remove this line and handle your textfield separately.

orkenstein
  • 2,810
  • 3
  • 24
  • 45
  • 1
    Yup. It actually contains all those 5 fields. Forgot to mention it. Anyway, I handle the textfields separately and it works fine now. Thanks for the explanation. :) – Faiz Mokhtar Sep 30 '14 at 09:37
0
- (void)viewDidLoad {
   [super viewDidLoad];


   [self.segmentCtrl addTarget:self
                            action:@selector(segmentChanged:)
                  forControlEvents:UIControlEventValueChanged];

    //Tag=5 is of phoneTextField
   [self.view viewWithTag:5].userInteractionEnabled = NO;

}


- (IBAction)segmentChanged:(id)sender
{
    int selectedIndex = (int)[self.segmentCtrl selectedSegmentIndex];

   NSLog(@"SEGMENT: %d",selectedIndex );


   switch (selectedIndex)
   {
       case 0:
                    // Takeaway
        [self.view viewWithTag:5].userInteractionEnabled = NO;
            //Tag=5 is of phoneTextField
        break;

       case 1:
                    // Delivery
        [self.view viewWithTag:5].userInteractionEnabled = YES;
        //Tag=5 is of phoneTextField
        break;

       default:
        break;
   }

}
Gaurav Rami
  • 1,287
  • 14
  • 21