Objective-C : Auto Update Label text
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;
}
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.
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];
精彩评论