How to disable part of UISegmentedControl?

IphoneIosUisegmentedcontrol

Iphone Problem Overview


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

Iphone Solutions


Solution 1 - Iphone

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

[segmentedControl setEnabled:NO forSegmentAtIndex:0];

if you want to disable the first segment.

Solution 2 - Iphone

For those who can be interested in the swift command:

Swift 2.3

segmentedControl.setEnabled(false , forSegmentAtIndex: 0);

Swift 3

self.segmentedControl.setEnabled(false, forSegmentAt: 0);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPoojaView Question on Stackoverflow
Solution 1 - IphoneDeepak DanduproluView Answer on Stackoverflow
Solution 2 - IphoneVictor RiusView Answer on Stackoverflow