DLRadioButton icon indicating copy to clipboard operation
DLRadioButton copied to clipboard

Problem setting selected state

Open bbonetti opened this issue 7 years ago • 23 comments

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

bbonetti avatar Sep 30 '17 21:09 bbonetti

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.

DavydLiu avatar Sep 30 '17 23:09 DavydLiu

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?

bbonetti avatar Oct 01 '17 00:10 bbonetti

selected is an UIControl property. Please refer to Apple's doc

DavydLiu avatar Oct 01 '17 02:10 DavydLiu

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.

bbonetti avatar Oct 01 '17 04:10 bbonetti

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.

bbonetti avatar Oct 01 '17 04:10 bbonetti

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.

bbonetti avatar Oct 01 '17 04:10 bbonetti

Hi @DavydLiu, I am facing the same issue as mentioned by @bbonetti Please let me know if there is any way to achieve it.

majeedyaseen avatar Nov 11 '17 18:11 majeedyaseen

I achieve it in Swift using the following

dlRadioButtonName.sendActions(for: .touchUpInside)

cdeakin avatar Nov 14 '17 17:11 cdeakin

@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

DavydLiu avatar Nov 15 '17 06:11 DavydLiu

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

majeedyaseen avatar Nov 15 '17 06:11 majeedyaseen

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...

faulknerwn avatar Dec 08 '17 03:12 faulknerwn

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!

faulknerwn avatar Dec 08 '17 04:12 faulknerwn

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.

cbnewham avatar Dec 28 '17 15:12 cbnewham

Extending on cdeakin's comment, I added this extension:

extension DLRadioButton {
    func setSelected(_ selected: Bool) {
        if self.isSelected != selected {
            self.sendActions(for: .touchUpInside)
        }
    }
}

lhbooker avatar Jan 07 '18 02:01 lhbooker

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.

axiom-media avatar Apr 19 '18 07:04 axiom-media

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 avatar Apr 19 '18 16:04 axiom-media

@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.

DavydLiu avatar Apr 19 '18 21:04 DavydLiu

@bbonetti @majeedyaseen for Swift 4, please use isSelected to set the selected property.

DavydLiu avatar Apr 19 '18 22:04 DavydLiu

@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 avatar Apr 19 '18 22:04 axiom-media

@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.

DavydLiu avatar Apr 19 '18 23:04 DavydLiu

@cbnewham The inversion is only for multiple selectable buttons so that users can toggle a button. I moved the logic to touchUpInside.

DavydLiu avatar Apr 19 '18 23:04 DavydLiu

Having same issue in a table view. It's not working by using .isSelected.

PedroAnibarroTAO avatar May 14 '18 19:05 PedroAnibarroTAO

@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

diliptilonia avatar Aug 09 '18 06:08 diliptilonia