문제

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];
        }
    }
도움이 되었습니까?

해결책

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];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top