1

I need four buttons besides a four Labels instead of a Segmented Control with 4 buttons in the bottom of the VC. This is the code for the Segmented Control, I don't know how to set a button to activate the label to be populated by the datePicker. Any help is appreciated.

- (IBAction)didChangeDate:(UIDatePicker *)sender {
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"HH:mm"];

[formatter setTimeZone:[NSTimeZone localTimeZone]];

NSString *formattedDate = [formatter stringFromDate:self.datePicker.date];

switch (self.segmentedControl.selectedSegmentIndex) {
    case 0:
        _outLabel.text = formattedDate;
        outTime = self.datePicker.date;
        break;
    case 1:
        _inLabel.text = formattedDate;
        inTime = self.datePicker.date;
        break;
    case 2:
        _offLabel.text = formattedDate;
        offTime = self.datePicker.date;
        break;
    case 3:
        _onLabel.text = formattedDate;
        onTime = self.datePicker.date;
    default:
        break;
}

}

Manolo
  • 469
  • 6
  • 20

1 Answers1

1

Create Button with Tag

  [_buttonOne setTag:0];
  [_buttonTwo setTag:1];
  [_buttonThree setTag:2];
  [_buttonFour setTag:3];

Then you can add a single IBAction selector, bind selector with buttons:

 - (IBAction)buttonSelector:(UIButton *)sender;

Use the above switch-case to do conditional selection

switch ([sender tag]) {
    case 0:
        _outLabel.text = formattedDate;
        outTime = self.datePicker.date;
        break;
    case 1:
        _inLabel.text = formattedDate;
        inTime = self.datePicker.date;
        break;
    case 2:
        _offLabel.text = formattedDate;
        offTime = self.datePicker.date;
        break;
    case 3:
        _onLabel.text = formattedDate;
        onTime = self.datePicker.date;
    default:
        break;
}

You can also add code for select/Deselect in the above switch-case.

Hope it helps.:)

Meet
  • 4,904
  • 3
  • 24
  • 39
  • 1
    Hi Meet, I think it will work but I am doing something wrong, I set one IBAction and drag the 4 buttons with Tags to the same IBAction, I am getting 4 errors of use of undeclared identifier FormattedDate :( I just add the first block of code on top of the old one, can you please take a look? – Manolo May 08 '14 at 05:36
  • Please change the line "NSString *formattedDate = [formatter stringFromDate:self.datePicker.date];" to "NSString *formattedDate = [formatter stringFromDate:sender.date];" – Meet May 08 '14 at 06:50