0

i need to implement two segmented controls. The result will be different considering the index of the two segmented controls. Considering that in the first one each index has an int fixed number (not variable) and the second controls has an equation associated for each index. I need that the program understands which is the combination of the two controls (e.g. I = 0 & II = 1). I develop this code but is not working for me.

if ((effortSegmented.selectedSegmentIndex ==0) && (styleSegmented.selectedSegmentIndex ==0)) {


        float weight_b = [weightTf.text floatValue];
        float totalCal = (((704*weight_b)/70.38) - 12);
        self.calL.text=[NSString stringWithFormat:@"~%.1f kcal", totalCal];


    } else if ((effortSegmented.selectedSegmentIndex==0) && (styleSegmented.selectedSegmentIndex==1)) {

        float weight_b = [weightTf.text floatValue];
        float totalCal = (((704*weight_b)/70.38)+32);
        self.calL.text=[NSString stringWithFormat:@"~%.1f kcal", totalCal];


    } else if ((effortSegmented.selectedSegmentIndex==0) && (styleSegmented.selectedSegmentIndex==2)) {

        float weight_b = [weightTf.text floatValue];
        float totalCal = (((704*weight_b)/70.38)+51);
        self.calL.text=[NSString stringWithFormat:@"~%.1f kcal", totalCal];

    }

as you can see the [-12, 32, 51] are fixed int associated with the first control index. Running the program even if i change the control index one, it would not result in any change in the label. Don't know where to look.

PS I can't declare tags because in

-(void)awakefornib {
[segControl setSegmentedCount:3]
}

i can't see the setSegmentedCount. Is not available in the list.

Thanks

MrKinisia
  • 35
  • 5

3 Answers3

0

Set tag property to your Segmented controls and use it to identify which event belongs to which Seg control.

- (void)awakeFromNib
{
    [segControl setSegmentCount:3];
    [[segControl cell] setTag:0 forSegment:0];
    [[segControl cell] setTag:1 forSegment:1];
    [[segControl cell] setTag:2 forSegment:2];

}

Or for seg controls

self.mySegmentedControl.tag = 1;
NSNoob
  • 5,548
  • 6
  • 41
  • 54
  • You could do it like this as specified in Apple docs. Edited the answer – NSNoob Nov 29 '14 at 12:33
  • I saw the docs and when i am creating the -(void)awakefornib: { [segControl setSegmentedCount:3] i don't have the setSegmentedCount. Consequently i can't create tags. – MrKinisia Nov 29 '14 at 12:34
0

You can set tag where you declared or before your this method call is set like :

effortSegmented.tag=1;
styleSegmented.tag =2;
0

I solve in this way because i still have not understood how to do tags.

- (IBAction)switchValue:(id)sender {


double teta = effortSegmented.selectedSegmentIndex;
NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
[def setDouble:teta forKey:@"index"];
[def synchronize];



}

i set the value of the index as a variable. Store it. I did it in the action related to the segmented controls which was not working before (the number 1).

then i got the variable in the button action in this way:

- (IBAction)click_on:(id)sender {

NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
double gamma = [def integerForKey:@"index"];
int delta = gamma*50;

I called the variable and use it in the if statement.

if (styleSegmented.selectedSegmentIndex ==0) {

        float weight_b = [weightTf.text floatValue];
        float totalCal = ((704*weight_b)/70.38)+delta;
        self.calL.text=[NSString stringWithFormat:@"~%.1f kcal", totalCal];


}

In order to get a fixed value of the variable i multiply the index for a constant. The result is a number between 0 and the constant. This is the best way i found. I know is not properly correct but hope that helps someone else.

MrKinisia
  • 35
  • 5