Hiding or Moving a SegmentContoller

Hello, I tried for 3 weeks to solve this problem and it will stun me. What I'm trying to do is create a 3-part segment from an array, display it in a view at a specific position, and then remove it from the view when the “OFF” flag is set. Everything works except deleting a segment. It will even commute with (pickOne) and display segments in the label. What I cannot find is either one of two things: setHidden: YES or removeAllSegments. Any help would be greatly 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.

    }
}

Now I know that I need to “not” create and delete in the same function, and they gave him a hint to fix this issue, but I don’t know how to use the hint.

here is what was suggested.

, , . , .

, :

:

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

    [self setTheControlProperty:theControl];

    ...

}

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

.

+3
4

, , , UISegmentedControl. . , -, , . , . -showSegment, , / . , .

, heightSC . , . , UISegmentedControl, , , , , , .. , .

+2

pickOne. showSegment , .

- (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];

        }
     }
0

, . , Mythogen BrianSlick , . .

- , [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];
}
0
[yourSegment removeFromSuperview];

?

-1
source

Source: https://habr.com/ru/post/1789306/


All Articles