DLRadioButton
DLRadioButton copied to clipboard
Problem setting selected state
Hi David.
Thanks for sharing DLRadioButton. I'm using it in my project and have a few issues that I ran into. First, I'm using a single checkbox style button that I want the user to set or clear as needed to mark an item. I only have the one button but if I don't set isMultipleSelectionEnabled to true, it doesn't clear when I click the button a second time. So I set isMultipleSelectionEnabled to true and it works but it seems odd to do it that way.
Second, I can't seem to set the selected state in code. I'm using Swift 3 and if I set myButton.isSelected to any value (true or false) the button shows up as selected and the icon is filled in. I need to be able to clear and set this button programmatically. How can I do this?
Thanks,
Bill Bonetti
Hi @bbonetti, thanks for the question.
For question 1, unfortunately, it's a bit weird but it's how radio button
suppose to behave.
For question 2, try use selected
instead of isSelected
. Because isSelected
is the getter of selected
. Let me know if it works.
Thanks. I'm okay with your answer to the first question. For the second, selected is a method that takes no arguments and returns a pointer to a radio button. I can't set the selected state with that. Any other suggestions?
selected
is an UIControl
property. Please refer to Apple's doc
Thanks David, but while I can see selected in the documentation for UIControl, I can't set it, or at least I can't set it in Swift 3. When I look at the Swift documentation for isSelected, it has it as both a get and set:
var isSelected: Bool { get set }
So I can't figure out how to make this work and unfortunately I'm not going to be able to play with this further for 2 weeks as I'll be on the road without my computer. But please let me know if you have any other suggestions.
Thanks again.
Thanks David, but while I can see selected in the documentation for UIControl, I can't set it, or at least I can't set it in Swift 3. When I look at the Swift documentation for isSelected, it has it as both a get and set:
var isSelected: Bool { get set }
So I can't figure out how to make this work and unfortunately I'm not going to be ablate play with this further for 2 weeks as I'll be on the road without my computer. But please let me know if you have any other suggestions.
Thanks again.
One more thing. Just looked at the documentation again for selected and it's only available in Objective C. In Swift they call it isSelected. So that's what I've been trying to set and no matter whether I use true or false as the value, the button is always drawn as selected.
Hi @DavydLiu, I am facing the same issue as mentioned by @bbonetti Please let me know if there is any way to achieve it.
I achieve it in Swift using the following
dlRadioButtonName.sendActions(for: .touchUpInside)
@bbonetti for swift the setter is selected
, please refer to this doc.
isSelected
is the getter, not the setter, so setting it will not have any effect.
@majeedyaseen @cdeakin
isSelected Whether or not the element is selected.
selected A Boolean value indicating whether the control is in the selected state. @property(nonatomic, getter=isSelected) BOOL selected;
@DavydLiu @bbonetti
I have this same problem - the example program has the button changing when clicked on but I can't figure out how that is being achieved...
I figured out part of it I think - I loaded their example and on the buttons set on the menuboard I copied every single setting - colors and all. I noticed that their indicator size was different and when I increased the size I started seeing a dot in the button. I'm not sure why this is not by default but it seemed to solve it!
Well, "selected" is not correct. It doesn't exist in Swift 4. According to the editor in Xcode "selected has been replaced by isSelected". Furthermore, the code is wrong if you're using multi selection (checkboxes). Here is part of the code in DLRadioButton for isSelected:
(void)setSelected:(BOOL)selected { ... if (self.isMultipleSelectionEnabled) { [super setSelected:!self.isSelected]; } else {
Rather than using the value passed in, it's simply setting it to be the boolean NOT of the current value.
Please fix this - it doesn't work and is highly annoying!
PS: I hate this f***g language. isSelected as a setter. WTF. Swift/Objective C is awful. Rant over.
Extending on cdeakin's comment, I added this extension:
extension DLRadioButton {
func setSelected(_ selected: Bool) {
if self.isSelected != selected {
self.sendActions(for: .touchUpInside)
}
}
}
Following up on this issue, is there any chance of adding a public method to be able to programmatically set isSelected which would not include any animation and work in Swift 4? Because of your implementation, it is not possible to set the "selected" property in Swift 4 (as mentioned by others, the property has been renamed to isSelected and for whatever reason trying to set isSelected to some value does not work). When I tried the class extension by @lhbooker it works but it uses animation so looks funny when scrolling a tableView, and it also makes it so that I can't implement a delegate method on that same command. This is a great library so it would be great to get it working properly with Swift 4.
As a follow up to my previous message, I was able to accomplish my needs by creating the following extension:
extension DLRadioButton {
func setSelectedWithoutAnimation(_ selected: Bool) {
super.isSelected = selected
}
}
Note that for my needs, I am using a single radio button in each UITableViewCell, so I don't need to keep track of other buttons or switch other buttons on/off depending on the value of this button. I just make sure "isMultipleSelectionEnabled" is true and set the state directly using the super (i.e. UIButton) method for state selection. This bypasses all of the animation so that the button state is set immediately when called from UITableView's cellForRowAtIndexPath.
@axiom-media @lhbooker I just tested in Swift 4. By setting isSelected
you will be able to programmatically set the selection state of a radio button.
@bbonetti @majeedyaseen for Swift 4, please use isSelected
to set the selected
property.
@DavydLiu did your testing include placing an instance of DLRadioButton in a UITableView prototype cell in interface builder? In this case, at least in my experience, setting isSelected does not work, and even if it did there would be a short animation of the selection state changing which would look strange. After a few hours of trying to sort it out, my extension did the trick. Perhaps you could consider including something like that (which simply sets the selection state) in the master branch?
@axiom-media I didn't test in a UITableView. However, I don't think UITableView will make things different. Just updated DLRadioButton
to 1.4.12
and added sample lines for setting selection state programmatically.
@cbnewham The inversion is only for multiple selectable buttons so that users can toggle a button. I moved the logic to touchUpInside
.
Having same issue in a table view. It's not working by using .isSelected.
@axiom-media Hii Could you please write a proper code which can work with swift3 ~4 or so we can select or deselect the button