Alan Fletcher
Alan Fletcher

Reputation: 283

UISegmentedControl index doesnt update when touched

Hello I am having a problem updating the index of my segmented controller. The following code comes from the calcualate class.

 + (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;
}

This code is from the viewcontroller.m class

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

And this is from the 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

I have left out the main calculations to make this shorter. When I run this the selected segment always stays at 0.

Can someone see why this is happening and if possible tell me how I could fix.

Any help would be greatly appreciated.

Thanking You

Upvotes: 2

Views: 164

Answers (1)

jhilgert00
jhilgert00

Reputation: 5489

You aren't initializing an instance of the FVC viewController, just declaring it:

Change:

MBBFirstViewController *fVC

To:

MBBFirstViewController *fVC = [MBBFirstViewContoller alloc] init];

Upvotes: 1

Related Questions