How to use UIButton as Toggle Button?

前端 未结 5 2280
庸人自扰
庸人自扰 2020-12-15 05:03

I am trying to create a toggle button for each cell in my table. When pressed, it will change the image and when pressed again it will change the image again -- Toggle.

5条回答
  •  -上瘾入骨i
    2020-12-15 05:38

    You can do it in a very easy approach. First of all, in your viewDidLoad set tag for your button. Let's say self.toggleButton.tag = 111. Now in your button action function, you can toggle the button like :

    - (IBAction)toggling:(id)sender{
        if(self.toggleButton.tag == 111){
            //this is normal state
            [self.toggleButton setTitle:@"Less..." forState:UIControlStateNormal];
            self.toggleButton.tag = 222;
        }else{
            //selected state
            [self.toggleButton setTitle:@"More..." forState:UIControlStateNormal];
            self.toggleButton.tag = 111;
        }
    }
    

    You can change the image of the button like this [self.toggleButton setImage://some image forState:UIControlStateNormal];.It's the easiest way I think. Hope it will help.

提交回复
热议问题