UI分段控件在集合视图中不起作用

时间:2016-10-25 06:00:25

标签: ios objective-c xcode uicollectionview uisegmentedcontrol

您好我正在尝试在集合视图中触发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
}
}

以上代码对我不起作用。我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

UISwitch不是UISegmentedControl

- (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
}
}