0

Is it possible to create a delegate in a Category?

Let's say, i have a Category UIImageView (ImagePicker), in which i've create some methods and all work perfectly.

However, i need to add a delegate into this category, UIImageViewDelegate let's say. Because at some point in the UIImageView (ImagePicker) implementation, i need it to trigger a event, and i want the user to catch this event.

Since i'm creating a category, and properties are not allowed in the category. So it seems that there is no way for me to add such a delegate, because based on my knowledge, to have create a delegate, you always have to create a property in the interface like this:

@property (weak, nonatomic) id <UIImageViewDelegate> delegate; 

Does anyone have any idea on this?

EDIT:

The logic of this Category,

The function of this category is to enable the user to set the UIImage of the UIImageView, by choose a photo from the Photo.app. The category handles all the process including the UI interactions. Now i need a delegate to call back to user when the image is set.

Scott Zhu
  • 8,341
  • 6
  • 31
  • 38
  • 1
    Your question boils down to "how to create a property in a category." You need to use an associated object. See the answer to this SO question: http://stackoverflow.com/questions/8733104/objective-c-property-instance-variable-in-category – TotoroTotoro Aug 01 '14 at 17:33
  • 1
    Also consider wether a category or a wrapper object is a better implementation for your situation. – Wain Aug 01 '14 at 17:35
  • 1
    At this point you should consider creating a proper subclass of `UIImageView`. It's hard to know for sure since there aren't enough details about what you are doing. A category should only be used to add a new method or two. When you get to the point where you really have a specialized `UIImageView`, you need a subclass. – rmaddy Aug 01 '14 at 17:35
  • (Btw. you can define *properties* in a category if they are not backed up by an *instance variable*. So this does not help here :) – Martin R Aug 01 '14 at 17:37
  • @rmaddy the function of this category is to enable the user to set the UIImage of the UIImageView, by choose a photo from the Photo.app. The category handles all the process including the UI interactions. Now i need a delegate to call back to user when the image is set. – Scott Zhu Aug 01 '14 at 17:45
  • @triplegg Right. So at this point you have a specialized `UIImageView`. That's a good indication you should subclass `UIImageView` and put all of your additions and delegate in the subclass. Then use the subclass throughout your app where this is needed. – rmaddy Aug 01 '14 at 17:48
  • @triplegg I would also argue that you should not subclass `UIImageView`. Instead, create some sort of controller class. The controller class can be setup with a reference to a `UIImageView`. Then the controller does all of the communication between the image picker and the image view. – rmaddy Aug 01 '14 at 17:50
  • 1
    @rmaddy right, but it could be much cooler if it can be done by just a category. because the rest functions work perfectly except for the delegate – Scott Zhu Aug 01 '14 at 17:52
  • You don't have to have a `delegate` property to create or use a delegate. You do need some way for the object that is going to invoke the delegate to actually find the delegate, and a property is the most convenient way to do this. – Hot Licks Aug 01 '14 at 19:09
  • @HotLicks how can i create a delegate without having to declare a property ? – Scott Zhu Aug 02 '14 at 03:50
  • @triplegg - Well, you can *create* a delegate that doesn't store anything -- it might just be "pure procedure" methods that return a value based only on the inputs. And to *use* a delegate you simply need a place to store the delegate pointer, and that can be a simple instance variable -- does not need to be a property. – Hot Licks Aug 02 '14 at 12:17

0 Answers0