ill_always_be_a_warriors
ill_always_be_a_warriors

Reputation: 1566

Different UIButton accessibilityLabel depending on whether the button is selected

I want to have the accessibilityLabel of a UIButton to be one value when not selected, and one value when selected. What's the best way to do this?

Upvotes: 4

Views: 2790

Answers (1)

Tommy
Tommy

Reputation: 100632

My instinct is to answer: subclass UIButton and check self.selected within an overridden accessibilityLabel.

But you could also take advantage of the fact that UIControls use the [add/remove]Target semantics so they can post changes of state to arbitrarily many targets, not just a single delegate. You could create and attach a target that pushes a new accessbilityLabel on demand. If you drop down to the runtime level and use objc_setAssociatedObject you could associate the helper with the button directly, giving it the same lifetime and avoiding the need for anybody else to keep a reference to it. You should probably just use UIControlEventAllEvents rather than trying to write a little implicit table of which events may cause selection to change.

Upvotes: 1

Related Questions