문제

I have created my own custom filter view which I want to act like a UISegmentedControl. I have set it up as a view and then 3 UIButtons. I want to know how I can make it so when one is clicked the other two are deselected and the api call for that button is made which then reloads the UICollectionView beneath it.

Here is a picture of my view with the 3 buttons to hopefully better explain what I'm trying to do.

enter image description here

도움이 되었습니까?

해결책

Connect all your buttons to this IBAction and define tag to each button.

- (IBAction)pressButton:(id)sender {

    UIButton* button1 = (UIButton*)[self.view viewWithTag:99];
    UIButton* button2 = (UIButton*)[self.view viewWithTag:100];
    UIButton* button3 = (UIButton*)[self.view viewWithTag:101];

    [button1 setEnabled:NO];
    [button2 setEnabled:NO];
    [button3 setEnabled:NO];

    [button1 setSelected:NO];
    [button2 setSelected:NO];
    [button3 setSelected:NO];

    if([sender tag]==99){
        [button1 setSelected:YES];
        [button1 setEnabled:YES];
    }else if([sender tag]==100){
        [button2 setSelected:YES];
        [button2 setEnabled:YES];
    }else{
        [button3 setSelected:YES];
        [button3 setEnabled:YES];
    }

}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top