1

I would like to create a UIButton that toggles its image. Once clicked the image should change to another one and stay that way until clicked again.

I have added both images to the default and selected states of the button on interface builder.

Now, I have created a UIButton subclass. My idea is to intercept the tap to that button and change the button state. So I added this to the UIButton subclass, hoping one of these methods would be triggered by a tap.

- (void)sendAction:(SEL)action
                to:(id)target
          forEvent:(UIEvent *)event {

  [super sendAction:action to:target forEvent:event];

  // toggle state
  [self setSelected:![self isSelected]];

}


- (void)sendActionsForControlEvents:(UIControlEvents)controlEvents {
  [super sendActionsForControlEvents:controlEvents];

  // toggle state
  [self setSelected:![self isSelected]];

}


- (void)touchesEnded:(NSSet *)touches
           withEvent:(UIEvent *)event {

  [super touchesEnded:touches withEvent:event];
  // toggle state
  [self setSelected:![self isSelected]];
}

None of these methods are triggered. Any clues?

Duck
  • 34,902
  • 47
  • 248
  • 470
  • Is button hooked up to one of these in IB & is it set to the subclass in identity inspector? – mc01 Feb 11 '15 at 17:44
  • Maybe you should try a different approach, check this [question](http://stackoverflow.com/questions/13202161/why-shouldnt-i-subclass-a-uibutton) – Leonardo Feb 11 '15 at 17:55
  • what do you mean? The button was added on interface builder and an action was set there, by dragging to the class it is being used. – Duck Feb 11 '15 at 17:55

3 Answers3

1

I could be barking up the wrong tree (pun partially intended), but I created a Swift project that does what I think you're asking for. I realize you're using ObjC, but for sake of time on my part I made it in Swift. In any case, it'll give you somewhere to start.

Here is the repo on GitHub: Click Here
--> Note: You'll need >= Xcode 6 to run the project.

A few things to note in the project:
1 - Be sure and connect the Button on Main.storyboard to the class you've created.
2 - In Swift, the class MyButton: UIButton is the line that implements UIButton for the class. I'm certain there's an ObjC equivalent, this Stack Overflow question may be helpful syntactically: Click Here

Hopefully some of this is helpful!

Good luck,
Kyle

Community
  • 1
  • 1
kbpontius
  • 3,867
  • 1
  • 30
  • 34
  • Thanks. I just recreated everything and it is now working. It must be one of those Xcode issues. – Duck Feb 11 '15 at 18:12
0

My guess is that the first two aren't called because the button doesn't have any targets registered with selectors. The third not being called is a little surprising.

That being said, you should to try [self addTarget:self action:@selector(buttonTapped:) forControlEvents: UIControlEventsTouchUpInside].

InkGolem
  • 2,662
  • 1
  • 15
  • 22
  • the action is being set on interface builder. Surprisingly, if I add a breakpoint to the action, I see this on the thread: `[UIApplication sendAction:to:forEvent:]`, that in theory is the first method I have added. – Duck Feb 11 '15 at 17:54
-1

The only way this can happen is if you haven't called addTarget on the UIButton subclass.

Vinod Vishwanath
  • 5,821
  • 2
  • 26
  • 40