我正在尝试制作一个等式(在x代码中),其中滑块值被假设除以100然后乘以文本字段值(由用户输入)。结果应该显示在另一个文本字段中。 我的.h文件:
@property (weak, nonatomic) IBOutlet UISlider *slider;
@property (weak, nonatomic) IBOutlet UILabel *counter;
@property (weak, nonatomic) IBOutlet UILabel *label;
@property (weak, nonatomic) IBOutlet UITextField *numberfield1;
@property (weak, nonatomic) IBOutlet UITextField *numberfield2
- (IBAction)calculate:(id)sender;
- (IBAction)changeLabel;
我的.m文件:
- (IBAction)changeLabel {
_counter.text = [[NSString alloc] initWithFormat:@"%1.f",_slider.value];
}
- (IBAction)calculate:(id)sender {
float x = [_counter.text floatValue];
float y = 100;
float calc_result = x / y;
float calc_result2 = calc_result * _numberfield2; // (Invalid operand to binary expression ('float' and 'UITextField*'))
self.numberfield2.text = [NSString stringWithFormat:@"%0.1f", calc_result2];
}
在()中获取错误我做错了什么?我需要做些什么来修复它?还有其他缺陷吗?如果是这样我该怎么办呢? 先谢谢!
答案 0 :(得分:1)
试试这个:
- (IBAction)calculate:(id)sender {
float x = [_counter.text floatValue];
float y = 100;
float calc_result = x / y;
float numberField2Float = [_numberfield2.text floatValue]; //converts the string in the text field to a float
float calc_result2 = calc_result * numberField2Float;
self.numberfield2.text = [NSString stringWithFormat:@"%0.1f", calc_result2];
}