Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use UIButton as Toggle Button?

Tags:

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.

In the UIButton class I don't see a selected state.

I'm looking for a way to create a toggle button with UIButton so that I can change the state on each click.

This is how I'm doing it in rubymotion right now using rmq

@fav_button.on(:touch) do |sender|   puts "pressed fav button for id: " + data[:id] + " and name: " + data[:name]   #how do I change the state here? end 
like image 435
Anthony Avatar asked Apr 05 '14 15:04

Anthony


People also ask

What is a UIButton?

A control that executes your custom code in response to user interactions.


2 Answers

You can create toggle button easily, you just need to set respective images for respective states, after that, you can use the selected property to toggle between these images.

I made a pure objective-c code to show how you can do that, but you can set the images anyway in Storyboards ou Xibs too, check out:

// First, set the images for normal state and selected state [button setImage:normalImage forState:UIControlStateNormal]; [button setImage:selectedImage forState:UIControlStateSelected]; 


// Don't forget to add an action handler to toggle the selected property [button addTarget:self action:@selector(buttonTouch:withEvent:) forControlEvents:UIControlEventTouchUpInside]; 


// Now, in your button action handler, you can do something like this: - (void)buttonTouch:(UIButton *)aButton withEvent:(UIEvent *)event {   aButton.selected = !aButton.selected; } 

I hope this can help you.

like image 127
Jan Cássio Avatar answered Oct 02 '22 07:10

Jan Cássio


Swift 4.0 Solution (using Storyboards)

First, ensure the UIButton Type is set to Custom in the Attributes Inspector.

Ensure UIButton Type is set to Custom

// Reference to UIButton in Storyboard @IBOutlet weak var toggleButton: UIButton!  override func viewDidLoad() {     super.viewDidLoad()      // assumes you have two images in the bundle/project      // called normal.png and selected.png.     let normalImage = UIImage(named: "normal.png")     let selectedImage = UIImage(named: "selected.png")      toggleButton.setImage(normalImage, for: .normal)     toggleButton.setImage(selectedImage, for: .selected) } 

Below screenshot demonstrates reference to toggleButton in Storyboard, and Touch Up Inside Event, ie: a user tapping the button, which fires off didPressButton below.

enter image description here

@IBAction func didPressButton(_ sender: Any) {          // if the button was selected, then deselect it.     // otherwise if it was not selected, then select it.     toggleButton.isSelected = !toggleButton.isSelected      if toggleButton.isSelected {         print("I am selected.")      } else {         print("I am not selected.")     } } 
like image 27
JaredH Avatar answered Oct 02 '22 06:10

JaredH