Muthu Raj Muthu Raj - 1 month ago 7
Objective-C Question

UIsegmented control not working inside collection view

Hi I am trying to trigger events for UIsegment control inside the collection view.

here is my code.

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


The above code not works for me.Any help will be appreciated.

Answer

is not UISwitch it is UISegmentedControl do like

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