0

I am new to Objective-C and i need your help! In the code that you will see below, my labels pick data from a picker view. I would like instead of using a "calculate" IBAction, to have my "chargeLabel.text" updated in real time as soon as the user changes a value in the picker view.

-(IBAction)calculate:(id)sender {
float floatTime;
if ([typeLabel.text isEqualToString:@"NiMH"])
{
    floatTime=([mAhLabel.text floatValue]/chargerSlider.value)*1.5;
} else {
    floatTime=([mAhLabel.text floatValue]/chargerSlider.value)*1.4;
}
int intHourhs=(floatTime);
float floatHours=(intHourhs);
float floatMinutes=(floatTime-floatHours)*60;
NSString *stringTotal = [NSString stringWithFormat: @"Hours: %.0i Minutes: %.0f", intHourhs, floatMinutes];
chargeLabel.text=stringTotal;

}

Pantelis Proios
  • 1,359
  • 1
  • 20
  • 32

2 Answers2

2

You have to implement UIPickerViewDelegate Protocol. Then, perform whatever you want in didSelectRow method

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    ...
    //here your code

}

Check UIPickerViewDelegate Protocol Reference

EDIT: I just realized that your accessing value property of chargerSlider if it is a UISlider you don't need to implement any delegate, just assing your IBAction to the Value Changed selector of the slider in IB.

Youssef
  • 3,582
  • 1
  • 21
  • 28
1

Set yourself up as the delegate of UIPickerView, and implement this method: pickerView:didSelectRow:inComponent When this method get called, simply call [self calculate:nil];

0xSina
  • 20,973
  • 34
  • 136
  • 253