How to disable part of UISegmentedControl?

Pooja picture Pooja · Jun 15, 2011 · Viewed 22.4k times · Source

Following is the code for UISegmentedControl

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects: [UIImage imageNamed:@"down.png"], [UIImage imageNamed:@"dList.png"], nil]];  

[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];  
segmentedControl.frame = CGRectMake(0, 0, 90, 65);  
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;  
segmentedControl.momentary = YES;  

UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];  

[segmentedControl release];  

self.navigationItem.rightBarButtonItem = segmentBarItem;  

[segmentBarItem release];    

and

- (void)segmentAction:(id)sender{
    if([sender selectedSegmentIndex] == 0)
    {
        button.hidden=NO;
    }
    else
    {
        [self dListMethod ];
    }
}

In the if() section I want to disable selectedSegmentIndex==0 and enable when button.hidden=YES

Answer

Deepak Danduprolu picture Deepak Danduprolu · Jun 15, 2011

Use setEnabled:forSegmentAtIndex: method to enable and disable the segments.

[segmentedControl setEnabled:NO forSegmentAtIndex:0];

if you want to disable the first segment.