34

I've created a custom picker view type of control by subclassing UIView. I would like to be able to send a "UIControlEventValueChanged" control event from within this control, so that I can register for it in whichever view controller is using the control.

How can I can I get my custom control to trigger this event when I deem it should be triggered?

Christian Gossain
  • 5,942
  • 12
  • 53
  • 85
  • Protocols or via NSNotificationCenter is the way to go. Would personally choose protocols in this case. Have a look at http://iosdevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html – Groot Jun 29 '13 at 23:29

1 Answers1

70

Assuming your custom control extends UIControl, then you simply do:

[self sendActionsForControlEvents:UIControlEventValueChanged];

This will call all registered targets (via addTarget:action:forControlEvents: that this event has happened.

Use this function where you deem should trigger a value has changed

Cyril
  • 2,783
  • 1
  • 24
  • 35
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • All my controls extend UIView since UIControl extends UIView do you think I could simply switch all my control to subclass UIControl without having to change anything else? – Christian Gossain Jun 29 '13 at 23:38
  • 3
    Custom controls should always extend `UIControl`. This gives you all of the benefits of that class. Once you change your class to extend `UIControl`, the only thing you may have to do is strip out code you now get for free from `UIControl` that you weren't getting from `UIView`. – rmaddy Jun 29 '13 at 23:39