whatdoesitallmean
whatdoesitallmean

Reputation: 1586

Add data to UIView using Categories

I'd like to extend the iOS UIView class so I can store some of my own data and state within my UIView instances. I am new to iOS. I understand that I can't add instance variables to Categories, so presumably I can't add new state to my UIViews.

I am therefore thinking of creating an NSArray, where each of my UIViews has a unique tag which is then used as an index into an NSArray, and where each item in the NSArray holds the additional state information about that particular UIView. The drawback here is the management of the Tags and the NSArray. It would be so much more straightforward if I could just add state directly to any UIView via a Category.

Can anyone advise me on the best way to add some instance variables and state to UIViews? I am using lots of UIView subclasses, and so don't really want to start subclassing UIView.

Thanks.

Upvotes: 0

Views: 912

Answers (2)

Vladimir Gritsenko
Vladimir Gritsenko

Reputation: 1683

Associative references will let you do what you want, though I'm not sure why you don't want to subclass (you already say you have a lot of subclasses - perhaps a better strategy of subclassing would be a better solution?).

Upvotes: 1

tuxSlayer
tuxSlayer

Reputation: 2824

Just use UIView.tag property for that. Its NSInteger (long), so you can have a map somewhere else.

Upvotes: 0

Related Questions