ios - Sending NSNotifications to all objects of a class -
i have object can selected user click. current requirements of app, @ time, there no more 1 of these items selected @ point during app execution.
i implemented mechanism enforce this, follows:
- each of these objects has unique identifier property.
- when each object created, subscribes
nsnotificationcenter
listeningmy_object_selected
notification. - when each object selected, posts
my_object_selected
notification, unique id part ofuserinfo
dictionary. - then, when each object receives notification, checks see if id same 1 in userinfo. if is, nothing, if isn't, sets unselected.
is decent approach problem? if not, how it?
it decent way of doing it, although not efficient. more objects have, more time spend comparing ids. easiest way store object pointers , ids in map table (or similar) , remember last selected object. whenever select new object, clear selection flag of last selected object, new object , set selection flag. requires keep collection of objects, though.
the time required update selections approach independent of number of objects have.
Comments
Post a Comment