触摸时UISegmentedControl索引不会更新

时间:2012-11-17 00:55:59

标签: objective-c ios xcode model-view-controller uisegmentedcontrol

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

 + (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。

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

非常感谢任何帮助。

感谢你

1 个答案:

答案 0 :(得分:1)

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

变化:

MBBFirstViewController *fVC

致:

MBBFirstViewController *fVC = [MBBFirstViewContoller alloc] init];