0

我是 Objective-C 的新手,我需要你的帮助!在您将在下面看到的代码中,我的标签从选择器视图中选择数据。我不想使用“计算”​​IBAction,而是在用户更改选择器视图中的值时实时更新我的​​“chargeLabel.text”。

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

}

4

2 回答 2

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.

于 2011-10-03T10:40:13.107 回答
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];

于 2011-10-03T10:40:26.333 回答