2015-06-09 107 views
0

我一直在浏览网站,我一直无法找到任何答案我的问题。在我的应用程序中,我有一个模式对话框,显示使用自定义单元格的UICollectionView,并且只包含一个UIButton。多个按钮,但一次只能按一个按钮

我已经设置了按钮可以选择,而不是单击。我想只允许一次选择一个按钮,并且当选择一个按钮时,我希望该部分中的其他按钮被禁用。我不确定如何去解决这个问题,因为我对IOS还很陌生。下面的屏幕截图显示了模型和按钮的基本外观。

目前我可以选择多个按钮,但我需要它是单个选择,当选择了一个按钮时,其余的都被禁用。目前,我将自定义的UICollectionViewCell中的选定内容交给了该类,并将其包含在下面。任何帮助和建议将是伟大的,我只需要指出正确的方向。

- (IBAction)Selected:(id)sender { 
    if(_Button.selected == 0) 
    { 
     [_Button setSelected:YES];   
    } 
    else 
    { 
     [_Button setSelected:NO]; 
    } 
} 

enter image description here

+0

尝试设置所有按钮的专属触摸 - [按钮setExclusiveTouch:YES]; – developer

+0

@developer我试过了,我通过集合视图中的所有可见单元格来访问按钮并将setExclusiveTouch设置为yes,但它仍然是多选择可悲的。 –

回答

0

在集合视图数据源方法 - collectionView:cellForItemAtIndexPath:当所配置的小区,设定indexPath项目值作为在细胞中相应的按钮的代码。

然后保留一个属性说,previousSelectedButtonIndex其中你可以维护之前选择的索引的值。

在您的按钮点击处理程序中,如果previousSelectedButtonIndex包含一个值,并且如果是,取消选择该按钮,请选择新按钮并将previousSelectedButtonIndex设置为新值。

请注意,您可能需要做一些错误处理,但基本上这是如何实现的。

0

声明一个全局变量如

NSInteger selectedIndex; 

值也设置-1到selectedIndex如最初没有按钮被选择。

selectedIndex = -1; 

然后写在UICollectionView

- (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    //here create cell object of UICollectionViewCell 
    cell.button.tag = indexPath.row; 
    [cell.button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    if(indexPath.row == selectedIndex) 
    { 
     //show this button as selected by setting different name/color/image for button 
     cell.button.enabled = TRUE; 
    } 
    else 
     cell.button.enabled = FALSE; 
    return cell; 
} 


- (void) buttonPressed:(id)sender 
    { 
    UIButton *selectedButton = (UIButton *)sender; 
    if(selectedIndex == selectedButton.tag) 
     selectedIndex = -1; //deselect the selected button 
    else 
     selectedIndex = selectedButton.tag; //select the tapped button 
    [self.collectionView reloadData]; 
    } 
0

数据源的方法你或许应该使用的按钮的集合。苹果已经赋予了同时控制一系列按钮的能力。尝试阅读以了解如何实现这一目标。 Outlet Collections