gpt4 book ai didi

ios - 如何通过滑动手势改变分段控件的值?

转载 作者:行者123 更新时间:2023-11-29 10:59:07 24 4
gpt4 key购买 nike

如何通过手势改变segmentedcontrol的值?

“无效参数不满足: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];
}
}

最佳答案

您需要先检查新索引是否有效。如果您已经在第一个选项卡上,则无法向左移动;如果您在最后一个选项卡上,则无法向右移动。所以在设置selectedSegmentIndex属性之前需要先计算新的索引,然后检查它是否在范围内:

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];
}

关于ios - 如何通过滑动手势改变分段控件的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16856819/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com