UISegmentedControl和switch语句的问题

时间:2012-01-10 19:30:37

标签: ios uiscrollview uisegmentedcontrol

我正在尝试开发这个应用程序,我真的很难过。我正在尝试使用UISegmentedControl加载一组UIScrollViews作为图像过滤器。

除了UIScrollViews存在巨大问题(这就是为什么在每个片段中单独创建新片段的原因)之外,单击片段1会加载所有6个UIScrollViews并且只允许我滚动第一个。当我选择最后一个段时,只加载了1个UIScrollView ...

我真的不知道我在这里做错了所以任何建议都会很棒!

非常感谢,

劳伦斯

~~~~下面的代码~~~

- (void)layerSelected:(id)sender
{
    int index = filterControl.selectedSegmentIndex;

switch (index)
{
    case 0: if (scroll == nil)
            {   
                scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                scroll.pagingEnabled = YES;
                scroll.delaysContentTouches = NO;
                scroll.userInteractionEnabled = YES;
                NSInteger viewCount = 15;
                for (int i = 0; i < viewCount; i++) 
                {
                    CGFloat yOrigin = i * self.view.frame.size.width;
                    UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
                    [filterViewOverlay setImage:[filterManager objectAtIndex:i]];
                    [scroll addSubview:filterViewOverlay];
                    [filterViewOverlay release];
                }
                scroll.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
                [self.scroll setContentOffset:currentPos1];
                [self.view addSubview:scroll];
                [scroll release];
            }
            else
            {
                //Set the position to the correct one
            }
    case 1: if (scroll1 == nil)
            {   
                scroll1 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                scroll1.pagingEnabled = YES;
                scroll1.delaysContentTouches = NO;
                scroll1.userInteractionEnabled = YES;
                NSInteger viewCount = 15;
                for (int i = 0; i < viewCount; i++) 
                {
                    CGFloat yOrigin = i * self.view.frame.size.width;
                    UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
                    [filterViewOverlay setImage:[filterManager objectAtIndex:i]];
                    [scroll1 addSubview:filterViewOverlay];
                    [filterViewOverlay release];
                }
                scroll1.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
                [self.scroll1 setContentOffset:currentPos2];
                [self.view addSubview:scroll1];
                [scroll1 release];
            }
            else
            {
                //Set the position to the correct one
            }
    case 2: if (scroll2 == nil)
            {   
                scroll2 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                scroll2.pagingEnabled = YES;
                scroll2.delaysContentTouches = NO;
                scroll2.userInteractionEnabled = YES;
                NSInteger viewCount = 15;
                for (int i = 0; i < viewCount; i++) 
                {
                    CGFloat yOrigin = i * self.view.frame.size.width;
                    UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
                    [filterViewOverlay setImage:[filterManager objectAtIndex:i]];
                    [scroll2 addSubview:filterViewOverlay];
                    [filterViewOverlay release];
                }
                scroll2.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
                [self.scroll2 setContentOffset:currentPos3];
                [self.view addSubview:scroll2];
                [scroll2 release];
            }
            else
            {
                //Set the position to the correct one
            }
    case 3: if (scroll3 == nil)
            {   
                scroll3 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                scroll3.pagingEnabled = YES;
                scroll3.delaysContentTouches = NO;
                scroll3.userInteractionEnabled = YES;
                NSInteger viewCount = 15;
                for (int i = 0; i < viewCount; i++) 
                {
                    CGFloat yOrigin = i * self.view.frame.size.width;
                    UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
                    [filterViewOverlay setImage:[filterManager objectAtIndex:i]];
                    [scroll3 addSubview:filterViewOverlay];
                    [filterViewOverlay release];
                }
                scroll3.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
                [self.scroll3 setContentOffset:currentPos4];
                [self.view addSubview:scroll3];
                [scroll3 release];
            }
            else
            {
                //Set the position to the correct one

            }
    case 4: if (scroll4 == nil)
            {   
                scroll4 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                scroll4.pagingEnabled = YES;
                scroll4.delaysContentTouches = NO;
                scroll4.userInteractionEnabled = YES;
                NSInteger viewCount = 15;
                for (int i = 0; i < viewCount; i++) 
                {
                    CGFloat yOrigin = i * self.view.frame.size.width;
                    UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
                    [filterViewOverlay setImage:[filterManager objectAtIndex:i]];
                    [scroll4 addSubview:filterViewOverlay];
                    [filterViewOverlay release];
                }
                scroll4.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
                [self.scroll4 setContentOffset:currentPos5];
                [self.view addSubview:scroll4];
                [scroll4 release];
            }
            else
            {
                //Set the position to the correct one

            }
    case 5: if (scroll5 == nil)
            {   
                scroll5 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                scroll5.pagingEnabled = YES;
                scroll5.delaysContentTouches = NO;
                scroll5.userInteractionEnabled = YES;
                NSInteger viewCount = 15;
                for (int i = 0; i < viewCount; i++) 
                {
                    CGFloat yOrigin = i * self.view.frame.size.width;
                    UIImageView *filterViewOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(yOrigin, 0, self.view.frame.size.width, self.view.frame.size.height)];
                    [filterViewOverlay setImage:[filterManager objectAtIndex:i]];
                    [scroll5 addSubview:filterViewOverlay];
                    [filterViewOverlay release];
                }
                scroll5.contentSize = CGSizeMake(self.view.frame.size.width * viewCount, self.view.frame.size.height);
                [self.scroll5 setContentOffset:currentPos6];
                [self.view addSubview:scroll5];
                [scroll5 release];
            }
            else
            {
                //Set the position to the correct one
            }
}
}

1 个答案:

答案 0 :(得分:0)

你错过了休息声明。 Switch需要在每个case语句的末尾使用break语句。否则,无论案例值如何,它都会在第一次成功匹配后执行所有代码。即

switch (soemthing) {
   case 1: // do something
      break;
   case 2: // do something else
      break;
   default:
}

按预期工作。如果你放弃休息,那么案例1将执行它下面的所有代码。