Hi all.
This time I want to add and remove segments to my segment control. I have no problems with adding more segments, but app crashes while removing them. Here's my code:
This method is called while any segment on segment control is pressed. It runs well until I try to remove one of the segments. I noticed that it crashes only if I try to remove existing segments (at index 0 or 1). According to documentation, every value greater than numberOfSegments should be capped to numberOfSegments - 1, so it should also crash if I try to remove segment at index 2,3,4..and so on, but somehow it doesn't crash then. Oh and by the way, the crash is caused by index being out of bounds:
If I try to remove segment at index 2,3,4 and so on, nothing happens Any ideas?
This time I want to add and remove segments to my segment control. I have no problems with adding more segments, but app crashes while removing them. Here's my code:
Code:
- (void)upperControlChangeState:(id)sender {
UISegmentedControl* seg = (UISegmentedControl*)sender;
NSLog(@"First - selected segment index: %i number of segments: %i",seg.selectedSegmentIndex,seg.numberOfSegments);
if(seg.selectedSegmentIndex == 0) {
if(seg.numberOfSegments == 1)
[seg insertSegmentWithTitle:NSLocalizedString(@"Back",@"") atIndex:1 animated:YES];
}
else if([seg numberOfSegments]>1) {
[seg removeSegmentAtIndex:1 animated:YES];
}
NSLog(@"After - selected segment index: %i number of segments: %i",seg.selectedSegmentIndex,seg.numberOfSegments);
}
This method is called while any segment on segment control is pressed. It runs well until I try to remove one of the segments. I noticed that it crashes only if I try to remove existing segments (at index 0 or 1). According to documentation, every value greater than numberOfSegments should be capped to numberOfSegments - 1, so it should also crash if I try to remove segment at index 2,3,4..and so on, but somehow it doesn't crash then. Oh and by the way, the crash is caused by index being out of bounds:
Code:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1)'