我是 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;
}
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;
}
您必须实现 UIPickerViewDelegate 协议。
然后,在
didSelectRow
方法中执行您想要的操作检查 "="">UIPickerViewDelegate 协议参考
编辑: 我刚刚意识到您的访问
chargerSlider
的value
属性,如果它是UISlider
,则不需要实现任何委托,只需将 IBAction 分配给Value更改了 IB 中滑块的选择器。
You have to implement
UIPickerViewDelegate
Protocol.Then, perform whatever you want in
didSelectRow
methodCheck UIPickerViewDelegate Protocol Reference
EDIT: I just realized that your accessing
value
property ofchargerSlider
if it is aUISlider
you don't need to implement any delegate, just assing your IBAction to theValue Changed
selector of the slider in IB.