hodji
hodji

Reputation: 82

Hiding or moving SegmentContoller

Hello I've tried for 3 weeks to solve this issue and it stumps me. What i am trying to do is create a 3 part segment from an array, display it in a view in a certain position, then remove it from view when the "OFF" flag is set. Every thing works except the removal of the segment. It will even commuticate with (pickOne) and display the segment letters in a label. What i can't get to work is either of the two: setHidden:YES, or removeAllSegments. Any help would be appreciated. Here is my code.

- (void) showSegment {

    int x = 192; 
    int y = 212;

    int w = 125;
    int h = 25;

    SegUnit1 = @"A";
    SegUnit2 = @"B";
    SegUnit3 = @"C";

    threeSegs = [NSArray arrayWithObjects: SegUnit1, SegUnit2, SegUnit3, nil];  
    segSize = [NSArray arrayWithArray:threeSegs];

    UISegmentedControl *heightSC = [[UISegmentedControl alloc] initWithItems:segSize];  

    if ([segmentState_height isEqualToString:@"ON"]) {

        NSLog(@"segmentState_height = %@",segmentState_height); 
        heightSC.frame = CGRectMake(x, y, w, h);    
        heightSC.segmentedControlStyle = UISegmentedControlStyleBar;
        heightSC.selectedSegmentIndex = -1;
        [heightSC addTarget:self
                     action:@selector(pickOne:)
           forControlEvents:UIControlEventValueChanged];
        [self.view addSubview:heightSC];
        [heightSC release]; 
    }   else if ([segmentState_height isEqualToString:@"OFF"]) {

        NSLog(@"segmentState_height = %@",segmentState_height);
        [heightSC setHidden:YES]; // NSLog showing "OFF" but segment will not hide. 
        [heightSC removeAllSegments]; // NSLog showing "OFF" and segment is suppose to dismantle and does not.

    }
}

I know now that i have to "not" create and remove in the same function, and was given a tip on correcting this but I don't know how to use the tip.

here is what was suggested.

Well, your method is a little confused, since you are trying to both create and hide at the same time. So you might consider splitting that up into separate methods.

In general, it will be along these lines:

Code:

if ([self theControlProperty] == nil)
{
    UISeg... *theControl = [[UISeg alloc] ....];

    [self setTheControlProperty:theControl];

    ...

}

if (shouldHideTheControl)
{
    [[self theControlProperty] setHidden:YES];
}

Any help would be appreciated.

Upvotes: 3

Views: 194

Answers (4)

hodji
hodji

Reputation: 82

I tried this and got the results I was looking for. Thanks goes to Mythogen and BrianSlick I just need to check and make sure there are no leaks. Now that will be a task.

Does anyone know if I need the second [heightSC release]; ?

// .h

@ interface ------ {
UISegmentedControl *segmentPicked;
}

|

@property (nonatomic, retain) UISegmentedControl *segmentPicked;

// .m

|

@synthesize segmentPicked;

|

if ([self segmentPicked] == nil) {

    UISegmentedControl *heightSC = [[UISegmentedControl alloc] initWithItems:segSize];  
    [self setSegmentPicked:heightSC];
    [heightSC release]; 
    heightSC.frame = CGRectMake(x, y, w, h);    
    heightSC.segmentedControlStyle = UISegmentedControlStyleBar;
    heightSC.selectedSegmentIndex = -1;
    [heightSC addTarget:self
    action:@selector(pickOne:)
    forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:heightSC];
    [heightSC release];
}

if ([segmentState_height isEqualToString:@"OFF"])
{
    [[self segmentPicked] setHidden:YES];
} else {
    [[self segmentPicked] setHidden:NO];
}

Upvotes: 0

user599005
user599005

Reputation: 1

Try using the remove segments in your button choice method pickOne. This takes it outside the showSegment method and matches the users desired action to make the change and clear off the buttons.

- (void) pickOne:(id)sender {

    UISegmentedControl* userChose = sender; 

    if( [userChose selectedSegmentIndex] == 0 ){

        your first button operation;
        [heightSC removeAllSegments];
    }

    if( [userChose selectedSegmentIndex] == 1 ){

        your second button operation;
        [heightSC removeAllSegments];

        }   
    if( [userChose selectedSegmentIndex] == 2 ){

        your third button operation;
        [heightSC removeAllSegments];

        }
     }

Upvotes: 0

Alice Isabelle
Alice Isabelle

Reputation: 618

The problem you have is that you're creating a new UISegmentedControl instance every time that method is called. The first time through, you create an instance and add it as a subview to your view. This apparently works fine, as it should. Then the method returns, and you no longer have any easy way to refer to that instance that you created. When you re-enter -showSegment, you create a different instance, and then hide and/or destroy it. This different instance has no effect whatsoever on the instance that you gave to the view.

What you need to do is make heightSC an instance variable. Add it to the interface declaration in the header file, then initialize it only once, and hide or modify it as needed subsequently. The key point is that you need to have a reference to the instance of the UISegmentedControl which is being drawn on the screen, a reference that lives outside the method itself that you can use the second, third, fourth, etc time you call that method.

Upvotes: 2

Ratinho
Ratinho

Reputation: 298

[yourSegment removeFromSuperview];

?

Upvotes: -1

Related Questions