2

您好,我在更新分段控制器的索引时遇到问题。以下代码来自calcualate 类。

 + (NSString *) calculate:(NSString *)height:(NSString *)weight{
  double bmiVal = 0;
   MBBFirstViewController *fVC;
        NSLog(@"%d", [[fVC heightSegment]selectedSegmentIndex]);
        NSLog(@"%d", [[fVC weightSegment]selectedSegmentIndex]);
        if ([[fVC heightSegment]selectedSegmentIndex] == 0 && [[fVC weightSegment]selectedSegmentIndex] == 0) {
             value = 10.5555;
        }else if ([[fVC heightSegment] selectedSegmentIndex] == 0 && [[fVC weightSegment]selectedSegmentIndex] == 1) {
             value = 11.5555; 
        }else if ([[fVC heightSegment]selectedSegmentIndex] == 0 && [[fVC weightSegment]selectedSegmentIndex] == 2) {
             value = 12.5555;
        }else if ([[fVC heightSegment]selectedSegmentIndex] == 1 && [[fVC weightSegment]selectedSegmentIndex] == 0) {
             value = 13.5555;
        }else if ([[fVC heightSegment]selectedSegmentIndex] == 1 && [[fVC weightSegment]selectedSegmentIndex] == 1) {
             value = 14.5555;
        }else if ([[fVC heightSegment] selectedSegmentIndex] == 1 && [[fVC weightSegment]selectedSegmentIndex] == 2) {
             value = 15.55555;
        }else if ([[fVC heightSegment]selectedSegmentIndex] == 2 && [[fVC weightSegment]selectedSegmentIndex] == 0) {
             value = 16.55555;
        }else if ([[fVC heightSegment]selectedSegmentIndex] == 2 && [[fVC weightSegment]selectedSegmentIndex] == 1) {
             value = 17.55555;
        }else if ([[fVC heightSegment]selectedSegmentIndex] == 2 && [[fVC weightSegment]selectedSegmentIndex] == 2) {
             value = 18.5555;
    }
}
 NSString *bmiV = [NSString stringWithFormat:@"%.2f",bmiVal];
return bmiV;
}

此代码来自 viewcontroller.m 类

- (IBAction)calculateTotal:(id)sender {
    NSString *h = [heightField text];
    NSString *w = [weightField text];
    yourBmiField.text = [calculate yourCalculation:h:w];
}

这是来自 viewcontroller.h

 @interface MBBFirstViewController : UIViewController <UITextFieldDelegate>{
     UITextField *heightField;
     UISegmentedControl *heightSegment;
     UITextField *weightField;
     UISegmentedControl *weightSegment;
 }
 @property (strong, nonatomic) IBOutlet UITextField *heightField;
 @property (strong, nonatomic) IBOutlet UISegmentedControl *heightSegment;
 @property (strong, nonatomic) IBOutlet UITextField *weightField;
 @property (strong, nonatomic) IBOutlet UISegmentedControl *weightSegment;
 - (IBAction)calculateTotal:(id)sender;
@end

我省略了主要计算以缩短它。当我运行这个时,选定的段总是保持在 0。

有人可以看到为什么会这样,如果可能的话,告诉我如何解决。

任何帮助将不胜感激。

感谢您

4

1 回答 1

1

您没有初始化 FVC viewController 的实例,只是声明它:

改变:

MBBFirstViewController *fVC

至:

MBBFirstViewController *fVC = [MBBFirstViewContoller alloc] init];
于 2012-11-17T02:22:01.167 回答