【问题标题】:How to correctly switch between two UIViews by using segment control ios?如何使用段控制 ios 在两个 UIView 之间正确切换?
【发布时间】:2014-09-05 20:59:42
【问题描述】:

我正在尝试使用segmentControl 在两个UIViews 之间切换。现在这两个UIViews 都嵌入了它们自己的scrollView。这些UIViews 应该显示为一个,所以我已经放置了它们在彼此之上(在XIB中)。当点击segmentControl时,我试图相应地隐藏/显示它们。但到目前为止,我无法在两者之间切换。还尝试了下面建议的解决方案。但是它只适用于 random 单元格并且无法在所有单元格中切换。缺少什么?

这就是我设置隐藏在 didSelectRow 中的上部视图的方式,我在其中展开 tableViewCell

 -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


if ([self.expandedCells containsObject:indexPath]) {

    expCell.upperContainer.hidden = NO;
    expCell.upperScroll.hidden = NO;
    [self.expandedCells removeObject:indexPath];

     }else{
    isExpanded=YES;
    [self.expandedCells addObject:indexPath];

    //hide upper container

    if (!expCell.upperContainer.hidden) {

        expCell.upperContainer.hidden = YES;


    }
    if (!expCell.upperScroll.hidden) {

        expCell.upperScroll.hidden =YES;
    }

   }

  [self.bTableView beginUpdates];
  [self.bTableView reloadData];
  [self.bTableView endUpdates];
 }

在 segmentControl 上的 expandCell 中单击我正在执行以下操作。

-(void)selectDeckView:(UISegmentedControl*)sender{

if (sender.selectedSegmentIndex==0) {
     NSLog(@"segment 0");             //executes
     expCell.lowerDeckView.hidden=NO;
     expCell.lowerScrollView.hidden=NO;
     expCell.upperScroll.hidden=YES;
     expCell.upperContainer.hidden=YES;

}else if (sender.selectedSegmentIndex==1){
    NSLog(@"segment 1");                //executes
    expCell.lowerDeckView.hidden=YES;
    expCell.lowerScrollView.hidden=YES;
    expCell.upperContainer.hidden=NO;
    expCell.upperScroll.hidden=NO;


}

}

【问题讨论】:

    标签: ios objective-c uitableview uiview uisegmentedcontrol


    【解决方案1】:

    我建议将每个视图放在自己的视图控制器中,然后在按下段时添加适当的子视图控制器。

    包含分段控件的视图控制器将是容器视图控制器,它会有这样的方法添加两个内容视图控制器之一(每个内容视图控制器都包含您提到的视图之一),它会被调用当一个段被按下时:

    - (void) displayContentController: (UIViewController*) content; 
    {
       [self addChildViewController:content];                 // 1
       content.view.frame = [self frameForContentController]; // 2
       [self.view addSubview:self.currentClientView];
       [content didMoveToParentViewController:self];          // 3
    }
    

    您可以缓存您的子视图控制器实例,以便它们只创建一次,如果合适的话,这意味着它会很快。 Apple 关于内容/子视图控制器的文档:

    https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html

    【讨论】:

    • 是的,我也想过。不幸的是,我的视图现在和展开的表格单元格一样放在 XIB 中。在这种情况下,如果可能的话,我该如何添加子视图。
    • 您可以将它们拆分为单独的 nib,然后通过执行以下操作为每个 nib 创建一个子视图控制器: UIViewController *viewController = [[UIViewController alloc] initWithNibName:@"myNib" bundle:nil ];
    • 尝试您的解决方案。不过我有一个问题。因为我正在使用当前实现填充当前视图控制器中的视图。我可以从我的父视图控制器填充 childViewControllers 视图然后只是加载它们?或者还有什么事情要做?
    • 是的,你可以这样做。就个人而言,我可能会将模型对象传递给填充视图的子视图控制器(因此让每个子视图控制器都是 UIViewController 子类)。请记住,重要的是不仅要添加子视图控制器的视图,还必须调用 addChildViewController 和 didMoveToParentViewController,如我的回答中所述,否则您会遇到这种方法的一些问题。
    【解决方案2】:

    你可能有一个覆盖另一个的视图,你尝试过吗:

    if (sender.selectedSegmentIndex==0) {
         NSLog(@"segment 0");             //executes
         expCell.lowerDeckView.hidden=NO;
         expCell.lowerScrollView.hidden=NO;
         expCell.upperScroll.hidden=YES;
         expCell.upperContainer.hidden=YES;
         [self.view bringSubviewToFront:expCell.upperScroll]
         [self.view bringSubviewToFront:expCell.upperContainer]
    
    }else if (sender.selectedSegmentIndex==1){
        NSLog(@"segment 1");                //executes
        expCell.lowerDeckView.hidden=YES;
        expCell.lowerScrollView.hidden=YES;
        expCell.upperContainer.hidden=NO;
        expCell.upperScroll.hidden=NO;
        [self.view bringSubviewToFront:expCell.lowerScrollView]
        [self.view bringSubviewToFront:expCell.lowerDeckView]
    
    
    }
    
    }
    

    确保以正确的顺序将它们带到最前面。

    【讨论】:

    • 它实际上只在第一个单元格中工作..之后在后续单元格中不会切换。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多