is it possible to add a custom event for a property value change?. I need to trigger a event when the value of a property changes.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I assume you want to trigger an event in an object (called “observer”) when a property changes in another object (“observed”).
You can do this in two ways:
using the notification center, you can override the setter by posting a notification using any of NSNotificationCenter “postNotification:” methods, then all interested observers will be notified provided they registered themselves for this kind of notification.
using KVO (Key-Value Observing) you must register the observer explicitly with the instance that has the property; e.g. if self wants to register to “instanceToObserve” property “myProperty”:
and then implement in the “self”‘s class the “observation” function:
What to use of the two is up to you. Consider that KVO is automatically supported if you follow basic Cocoa convention rules, so no extra effort is required and no change is needed to the synthesized setter.