这是代码。每当选择UIPickerView中的某些内容时,我都会尝试让按钮执行不同的操作。
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch(Striped Bass)
//Striped Bass
{
- (IBAction)calculateButtonPressed:(id)sender {
NSLog(@"Calculate Pressed");
float girth = [[self.girthTextField text] floatValue];
float length = [[self.lengthTextField text] floatValue];
NSLog(@"girth: %f length: %f", girth, length);
float weight = girth * girth * length / 800;
NSLog(@"Weight: %f", weight);
NSString *weightText = [NSString stringWithFormat:@"%f", weight];
self.weightTextField.text = weightText;
}
}
答案 0 :(得分:1)
你的问题根本不清楚,但无论如何我都会试一试。 我认为你对两件事感到困惑:
您按下一个按钮(可能)调用IBAction。 当您在pickerView中选择一行时,您会触发一个事件,该事件会被委托选中,并对其执行某些操作。
因此,在您的代码中,您正在另一个方法中定义一个方法。这是不可能的。 IBAction需要在didSelectRow
的定义之外定义。如果您确实需要运行该操作,则需要手动调用它。所以你的代码应该更像这样:
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch(Striped Bass)
//Striped Bass
[self calculateButtonPressed:pickerView];
}
- (IBAction)calculateButtonPressed:(id)sender {
NSLog(@"Calculate Pressed");
float girth = [[self.girthTextField text] floatValue];
float length = [[self.lengthTextField text] floatValue];
NSLog(@"girth: %f length: %f", girth, length);
float weight = girth * girth * length / 800;
NSLog(@"Weight: %f", weight);
NSString *weightText = [NSString stringWithFormat:@"%f", weight];
self.weightTextField.text = weightText;
}
现在,对我来说没有意义的是,你没有在方法的所有选定行中使用基于所选pickerView的东西。
无论如何,希望有所帮助。