Question

How change value of segmentedcontrol by swipegesture?

"Invalid parameter not satisfying: selectedSegmentIndex < (NSInteger)self._items.count"

    - (void)updateSelectedSegmentText 
    {
        int theSegmentIndex = [_segmentedControl selectedSegmentIndex];
        NSDictionary *theInfo = [self.top.segments objectAtIndex:theSegmentIndex];
        self.bioTextView.text = [NSString stringWithFormat:@"%@", theInfo [@"sData"]];
        [self.bioTextView scrollRangeToVisible:NSMakeRange(0, 0)];
    }

    - (void)swipe:(UISwipeGestureRecognizer *)swipeRecogniser
    {
        s = 1;
        if ([swipeRecogniser direction] == UISwipeGestureRecognizerDirectionLeft)
        {
            self.segmentedControl.selectedSegmentIndex -=s;
            [self updateSelectedSegmentText];
        }
        else if ([swipeRecogniser direction] == UISwipeGestureRecognizerDirectionRight)
        {
            self.segmentedControl.selectedSegmentIndex +=s;
            [self updateSelectedSegmentText];
        }
    }
Was it helpful?

Solution

You need to first check if the new index is valid. You cannot go to the left if you are already on the first tab and you cannot go to the right if you are on the last tab. So you need to first calculate the new index and then check if it is in range before you set the selectedSegmentIndex property:

NSInteger index = self.segmentedControl.selectedSegmentIndex;
if ([swipeRecogniser direction] == UISwipeGestureRecognizerDirectionLeft) {
    --index;
} else if ([swipeRecogniser direction] == UISwipeGestureRecognizerDirectionRight) {
    ++index;
}

if (0 <= index && index < self.segmentedControl.numberOfSegments) {
    self.segmentedControl.selectedSegmentIndex = index;
    [self updateSelectedSegmentText];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top