Objective-C:自动更新标签文本

发布于 12-07 21:51 字数 650 浏览 1 评论 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;

}

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;

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虚拟世界 2024-12-14 21:51:38

您必须实现 UIPickerViewDelegate 协议。
然后,在 didSelectRow 方法中执行您想要的操作

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

}

检查 "="">UIPickerViewDelegate 协议参考

编辑: 我刚刚意识到您的访问chargerSlidervalue 属性,如果它是 UISlider,则不需要实现任何委托,只需将 IBAction 分配给 Value更改了 IB 中滑块的选择器。

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.

挽心 2024-12-14 21:51:38

将自己设置为 UIPickerView 的委托,并实现此方法: pickerView:didSelectRow:inComponent
调用此方法时,只需调用 [selfcalculate:nil];

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文