Single selection radioButton - ios

So I have 3 buttons
override func viewDidLoad() {
super.viewDidLoad()
weeklyRadioButton.setImage(UIImage(named: "a"), for: .normal)
weeklyRadioButton.setImage(UIImage(named: "b"), for: .selected)
biWeeklyRadioButton.setImage(UIImage(named: "a"), for: .normal)
biWeeklyRadioButton.setImage(UIImage(named: "b"), for: .selected)
noThanksRadioButton.setImage(UIImage(named: "a"), for: .normal)
noThanksRadioButton.setImage(UIImage(named: "b"), for: .selected)
}
#IBAction func biWeeklyRadioButtonTapped(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
weeklyRadioButton.setImage(UIImage(named: "a"), for: .normal) .
// its not changing then image of the button.
noThanksRadioButton.setImage(UIImage(named: "a"), for: .normal)
}
and similar two Actions for other two buttons.
Current code allows multiple selection of button.
I want to know the way to implement to single selection in buttons.
so lets say i selected button 1
on selecting button 2, button 1 gets deselected

Try the below code. Please also make a note that all of the button outlets should be connected to the below action method.
#IBAction func anyRadioButtonTapped(_ sender: UIButton) {
//Reset all of them first
weeklyRadioButton.isSelected = false
biWeeklyRadioButton.isSelected = false
noThanksRadioButton.isSelected = false
//Highlight associate button
sender.isSelected = !sender.isSelected
}

Related

Setting UIButton Image With Ternary Operator

I have a password UICollectionViewCell with a UITextField to enter the password. I want to have a button where the user can change the isSecureTextEntry property.
My Code
private lazy var button: UIButton = {
let view = UIButton()
view.translatesAutoresizingMaskIntoConstraints = false
view.addTarget(self, action: #selector(toggle), for: .touchUpInside)
return view
}()
#objc func toggle(_ sender: UIButton) {
sender.isSelected == false ? button.setImage(secureImage, for: .normal) : button.setImage(unSecureImage, for: .selected)
}
Set Up Cell
self.toggle(button)
However, my button image is not changing.
sender.isSelected == false ? button.setImage(secureImage, for: .normal) : button.setImage(unSecureImage, for: .selected)
You're setting for normal state on one side and for selected state on the other.
Try this:
button.setImage((sender.isSelected ? unSecureImage : secureImage), for: .normal)
Can you try like this,
Setting the image for normal and selected state
On tapping the button changing the isSelected state of the button
private lazy var button: UIButton = {
let view = UIButton()
view.translatesAutoresizingMaskIntoConstraints = false
view.setImage(secureImage, for: .normal)
view.setImage(unSecureImage, for: .selected)
view.addTarget(self, action: #selector(toggleAction), for: .touchUpInside)
return view
}()
#objc func toggleAction(_ sender: UIButton) {
sender.isSelected.toggle()
}

How to select one checkbox at a time in swift?

I have implemented four checkboxes. Now, I want to select one checkbox at a time and if there's other checkbox selected, I want it deselected at the same time.
This is what I've tried but it selects all the checkboxes.
#IBAction func checkboxTapped(_ sender:UIButton){
if (sender.isSelected == false){
sender.setImage(UIImage(named: "selected"), for: UIControl.State.normal)
sender.isSelected = true;
} else {
sender.setImage(UIImage(named: "unselected"), for: UIControl.State.normal)
sender.isSelected = false;
}
}
Here, I have given same IBAction connection to all the checkboxes.
First create a set up function that sets the corresponding image for the button state.
(All code below was created in a playground so it might be a little different than yours)
private func setUp(button: UIButton) {
button.setImage(UIImage.init(systemName: "checkmark.circle"), for: .selected)
button.setImage(UIImage.init(systemName: "xmark.circle"), for: .normal)
button.addTarget(self, action: #selector(checkboxTapped), for: .touchUpInside) //Not needed if you use a storyboard
}
Then call it from viewDidLoad in your view controller (I have my buttons in an array buttons)
override func viewDidLoad() {
super.viewDidLoad()
buttons.forEach {
setUp(button: $0)
self.view.addSubview($0) //Not needed if you use a storyboard
}
buttons[0].isSelected = true
And then check your action method so it updates all buttons properly
#IBAction func checkboxTapped(_ sender:UIButton){
for button in buttons {
button.isSelected = false
}
sender.isSelected = true
}
If you are not using an array then replace my loops with calls to the individual buttons

Swift create custom logic for radio button

I have four custom UIButton, I applied button image radio (checked and checked). four button I have separate action method I can change the Image easily but if I checked first button another three button need uncheck. it should react like radio button.
Here, below my code
#IBAction func first_scheme(_ sender: Any) {
bRec = !bRec
if (bRec == true) {
firstscheme_button.setImage(UIImage(named: "uncheck.png"), for: .normal)
} else {
firstscheme_button.setImage(UIImage(named: "check.png"), for: .normal)
}
}
If you have 4 radio buttons at all times, you can put them in an array.
var radioButtons: [ButtonType] = [r1, r2, r3, r4]
You can now access the button in a loop and set the values for the other button to 'unchecked'.
func setRadioButtons(button: ButtonType) {
for radioButton in radioButtons {
if radioButton !== button {
radioButton.setImage(UIImage(named: "uncheck.png"), for: .normal)
}
}
}
#IBAction func first_scheme(_ sender: Any) {
bRec = !bRec
if bRec {
firstscheme_button.setImage(UIImage(named: "uncheck.png"), for: .normal)
} else {
firstscheme_button.setImage(UIImage(named: "check.png"), for: .normal)
}
setRadioButtons(button: sender)
}
alternate method
If all you want to do is check the button clicked and uncheck the other buttons, the logic is simpler.
Create the common action for all radio buttons as well create the IBOutletcollection for your all UIButtons ,
var radioButtons: [UIButton] = [r1, r2, r3, r4]
finally execute the common method as
func setRadioButtons(button: UIButton) {
for getradioButton in radioButtons {
getradioButton.setImage(UIImage(named: "uncheck.png"), for: .normal)
}
button.setImage(UIImage(named: "check.png"), for: .normal)
}
I suggest you set tags on the buttons with the simple tag property, and then you save it from a generic listener.
//first button selected
var lastTag = 0
#IBAction func first_scheme(_ sender: UIButton) {
buttonArray[lastTag].setImage(UIImage(named: "uncheck.png"), for: .normal)
lastTag = sender.tag
sender.setImage(UIImage(named: "check.png"), for: .normal)
}
You Can override isSelected variable in the custom UIButton and set the image depend on the isSelected value.
class customButton: UIButton {
override var isSelected: Bool {
willSet {
self.setImage(UIImage.init(named: "checked"), for: .normal)
}
didSet {
self.setImage(UIImage.init(named: "unchecked"), for: .normal)
}
}
}
After making 4 IBOutlets and 4 IBActions for the four custom UIButtons. you can easily select and unselect the buttons and apply your custom behaviour.
#IBAction func firstButtonAction(_ sender: Any) {
if let button = sender as? customButton {
button.isSelected = !button.isSelected
}
secondButton.isSelected = false
thirdButton.isSelected = false
fourthButton.isSelected = false
}

I want to change UIButton Image when tapped on it

I have 2 buttons in header on CollectionViewController. When i tap on one of them i'm changing an image of this buttons using UIControlState -> .normal .selected.
override func viewDidLoad() {
super.viewDidLoad()
collectionView.delegate = self
collectionView.dataSource = self
engSwitchButton.setImage(#imageLiteral(resourceName: "abc"), for: .normal)
geoSwitchButton.setImage(#imageLiteral(resourceName: "abg"), for: .normal)
engSwitchButton.setImage(#imageLiteral(resourceName: "abc2"),for: UIControlState.selected)
geoSwitchButton.setImage(#imageLiteral(resourceName: "abg2"), for: UIControlState.selected)
engSwitchButton.tag = Language.english.rawInt
geoSwitchButton.tag = Language.georgian.rawInt
}
#IBAction func languageSwitchTapped(_ sender: UIButton) {
sender.isSelected = !sender.isSelected
selectedLanguage = Language(rawInt: sender.tag)!
collectionView.reloadData()
}
I want the button which i tapped first, get back to .normal state when i'm changing state of 2d button with tapping on it.
To change a button image on tap you should use the UIControlState.highlighted.
So change:
engSwitchButton.setImage(#imageLiteral(resourceName: "abc2"),for: UIControlState.selected)
geoSwitchButton.setImage(#imageLiteral(resourceName: "abg2"), for: UIControlState.selected)
To this:
engSwitchButton.setImage(#imageLiteral(resourceName: "abc2"),for: UIControlState.highlighted)
geoSwitchButton.setImage(#imageLiteral(resourceName: "abg2"), for: UIControlState.highlighted)
#IBAction func yourFirstButton(_ sender: Any) {
firstButton.setImage(UIImage(named: "yourButtonPressedImage")!, for: .normal)
secondbutton.setImage(UIImage(named: "yourNormalImage")!, for: .normal)
}
and in your second buttons #IBAction method just switch the images for the buttons
#IBAction func yourSecondButton(_ sender: Any) {
secondButton.setImage(UIImage(named: "yourButtonPressedImage")!, for: .normal)
firsBbutton.setImage(UIImage(named: "yourNormalImage")!, for: .normal)
}

Select/deselect buttons swift xcode 7

Part way done with learning swift but I hit a small wall and yet again, I'm sure I'm just a bit new at this and an easy solution is there but I'm having trouble figuring out how to select/deselect buttons below is what I have so far and it is a button turns into a checkmark when clicked on... I've gotten that far but I need that button to deselect when clicked on again and then obviously be able to be clicked again if need be.
#IBAction func buttonPressed(sender: AnyObject) {
sender.setImage(UIImage(named: "Checkmark.png"), forState: .Normal)
}
Swift 3 note: .selected and .checked are now lower case UIControlState values in the SDK, and some of the methods have been renamed:
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), for: .normal)
button.setImage(UIImage(named: "Checked"), for: .selected)
You can also now use image literals with Xcode 8 instead of UIImage(named:):
#imageLiteral(resourceName: "Unchecked")
Swift 2:
Why not use the .Selected state of the button as the "checked" state, and the .Normal state as the "unchecked" state.
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), forState: .Normal)
button.setImage(UIImage(named: "Checked"), forState: .Selected)
// ...
#IBAction func buttonPressed(sender: AnyObject) {
if let button = sender as? UIButton {
if button.selected {
// set deselected
button.selected = false
} else {
// set selected
button.selected = true
}
}
}
You dont need to set selected in condition. I just doing with following method in swift:
func selectDeselect(sender: UIButton){
sender.selected = !sender.selected
if(sender.selected == true)
{
sender.setImage(UIImage(named:"select_heart"), forState: UIControlState.Normal)
}
else
{
sender.setImage(UIImage(named:"heart"), forState: UIControlState.Normal)
}
}
Here is Working code for swift 4.
Make Sure you need to connect Button IBAction Outlet as UIButton and set default button image from storyboard whatever you want.
#IBAction func btnTapped(_ sender: UIButton) {
if sender.currentImage == UIImage(named: "radio_unchecked"){
sender.setImage(UIImage(named: "radio_checked"), for: .normal)
}else{
sender.setImage(UIImage(named: "radio_unchecked"), for: .normal)
}
}
update for Swift 4+ :
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), forState: .Normal)
button.setImage(UIImage(named: "Checked"), forState: .Selected)
#IBAction func buttonPressed(sender: AnyObject) {
if let button = sender {
if button.isSelected {
// set deselected
button.isSelected = false
} else {
// set selected
button.isSelected = true
}
}
}
Set uncheck image on default state from storyboard and check image on selected state from storyboard.
#IBAction func buttonPressed(sender: AnyObject) {
buttonOutlet.isSelected = !buttonOutlet.isSelected
}
private(set) lazy var myButton: UIButton = {
let button = UIButton()
button.setImage(UIImage(named: "Unchecked"), for: .normal)
button.setImage(UIImage(named: "Checked"), for: .selected)
button.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
button.translatesAutoresizingMaskIntoConstraints = false
return button
}()
#objc
func buttonTapped(sender: AnyObject) {
sender.isSelected.toggle()
}
To select only the pressed button out of three buttons and deselect the others when designated button is pressed.
#IBAction func buttonPressed(_ sender: UIButton) {
// Deselect all buttons
button1.isSelected = false
button2.isSelected = false
button3.isSelected = false
// Select the pressed button
sender.isSelected = true
}

Resources