【问题标题】:UIsegmented control not working inside collection viewUIsegmented 控件在集合视图中不起作用
【发布时间】:2017-03-07 01:22:55
【问题描述】:

您好,我正在尝试在集合视图中触发 UIsegment 控件的事件。

这是我的代码。

CollectionViewCell.h

@property (strong, nonatomic) IBOutlet UISegmentedControl *mySegmentedControl;

ViewController.m
{  
NSInteger selectedSegment;
}

- (UIView *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
cell.mySegmentedControl.tag = indexPath.row;
selectedSegment = cell.mySegmentedControl.selectedSegmentIndex;
 [cell.mySegmentedControl addTarget:self action:@selector(segmentValueChanged:) forControlEvents:UIControlEventValueChanged];
 }

- (void) segmentValueChanged: (UISwitch *) sender {
//NSInteger index = sender.tag;
if(selectedSegment == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"!Alert"
                                                message:@"Do you think this property is not exists?"
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"Yes", nil];
[alert show];

}         
else
{
    //your code
}
}

上面的代码对我不起作用。任何帮助将不胜感激。

【问题讨论】:

  • 请发布collectionView:cellForItemAtIndexPath:的完整代码。您应该将segmentValueChanged: 中的selectedSegmentIndex 读入局部变量。
  • @Raj - 你试过在里面设置断点 - (void) segmentValueChanged:(UISwitch *) sender 。它只会在你点击分段控制时调用。

标签: ios objective-c xcode uicollectionview uisegmentedcontrol


【解决方案1】:

不是UISwitchUISegmentedControl点赞

- (void) segmentValueChanged: (UISegmentedControl *) sender {
//NSInteger index = sender.tag;
if(sender.selectedSegmentIndex == 0)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"!Alert"
                                                message:@"Do you think this property is not exists?"
                                               delegate:self
                                      cancelButtonTitle:@"Cancel"
                                      otherButtonTitles:@"Yes", nil];
[alert show];

}         
else
{
    //your code
}
}

【讨论】:

  • 使用 UIAlertController ,不推荐使用 UIALertView
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-01
  • 2013-01-24
  • 2015-09-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多