Question

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;
}

}

Était-ce utile?

La solution

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.:)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top