How to disable UISegmentedControl?

I have the following UISegmentedControl that I want to disable:

-(void)displayCheckMark { titleSegmentedControl = [[UISegmentedControl alloc] initWithItems:nil]; [titleSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"symbolbg.png"] atIndex:0 animated:YES]; [titleSegmentedControl insertSegmentWithImage:[UIImage imageNamed:@"inwatchlist.png"] atIndex:1 animated:YES]; [titleSegmentedControl addTarget:self action:@selector(titleBarButtonChanged:)forControlEvents:UIControlEventValueChanged]; titleSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; titleSegmentedControl.frame = CGRectMake(100,0,100,30); titleSegmentedControl.momentary = YES; titleSegmentedControl.tintColor = [UIColor blackColor]; self.navigationItem.titleView = titleSegmentedControl; [titleSegmentedControl setWidth:60 forSegmentAtIndex:0]; [titleSegmentedControl setTitle:symbol forSegmentAtIndex:0]; [titleSegmentedControl setWidth:30 forSegmentAtIndex:1]; [titleSegmentedControl setEnabled:NO]; } 

I don't have anywhere in the code. But I can still click on it and it will execute the action in the BarButtonChanged header:

How can I make sure it cannot be pressed?

+8
source share
3 answers

Try using:

 - (void)setEnabled:(BOOL)enabled forSegmentAtIndex:(NSUInteger)segment; 

or

 titleSegmentedControl.userInteractionEnabled = NO; 
+18
source

Inside titleBarButtonChanged:(id)sender add:

 if(!sender.enabled) return; 
+3
source

I wrote this method for this:

 func setEnabledSegmentedControl(enabled: Bool) { for segmentIndex in 1..<segmentedControl!.numberOfSegments { segmentedControl.setEnabled(enabled, forSegmentAt: segmentIndex) } } 
0
source

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


All Articles