removeSegmentAtIndex bug?

Discussion in 'iOS Programming' started by lawicko, Sep 1, 2008.

  1. macrumors member

    Joined:
    Jul 17, 2008
    #1
    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:
    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)'
    If I try to remove segment at index 2,3,4 and so on, nothing happens :confused: Any ideas?
     
  2. macrumors newbie

    Joined:
    Dec 13, 2008
    #2
    Solution?

    I am having the same issue. did you find a solution?
     
  3. thread starter macrumors member

    Joined:
    Jul 17, 2008
    #3
    Yes, I finally got it working but unfortunately I don't remember how I did it (no programming on mac for almost 2 months now...). I remember that I managed to remove only the first segment (index 0) and then adjust control to look as I wanted it to look (move labels left). It worked pretty well, I don't have an access to my mac code now, but will look at it when I'm home (in a week) an if it's still not solved then I'll post it here. Good luck.
     

Share This Page