Where to declare protocols for reuse in objective-c

If I want to reuse the protocol to notify the main view manager when the detail view manager is rejected, for example: Notify UIView when the modal UIImagePickerController is fired?

where can i declare this protocol? Is it better to use protocols in separate files? Thanks.

+6
source share
2 answers

Usually I (copying Apple methods) declare the protocols in the header of the class that will interact with the delegates of that protocol. (For example, the UIActionSheet header file contains the protocol declaration for the UIActionSheetDelegate.) It is technically not important where you declare the protocols unless you have a circular link to the #import statements. Usually this does not happen because the class that interacts with the protocol does this to avoid the need for #import all other classes that will now use the protocol. (The UIActionSheet sends messages to your classes when it is rejected by the protocol, and therefore #import of any of your classes is not needed.)

If you have a protocol and several unrelated classes will interact with delegates of this protocol, this will be a good indicator to put the protocol in your own file, because the protocol is not really associated with one particular class.

+3
source

I think you should declare the UIImagePickerController category in separate files. Like ImagePickerCtrlCatagory.h and ImagePickerCtrlCatagory.m In ImagePickerCtrlCatagory.h, do the following:

  #import <Foundation/Foundation.h> @protocol ImagePickerCtrlDelegate; @interface UIImagePickerController (DissappearDelegate){ id <ImagePickerCtrlDelegate> delegate; } @end 
+1
source

Source: https://habr.com/ru/post/917143/


All Articles