NSObject+A2BlockDelegate.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // NSObject+A2BlockDelegate.h
  3. // BlocksKit
  4. //
  5. #import <Foundation/Foundation.h>
  6. /** The A2BlockDelegate category extends features provided by A2DynamicDelegate
  7. to create custom block properties in a category on a delegating object and
  8. dynamically map them to delegate (`UIAlertViewDelegate`), data source
  9. (`UITableViewDataSource`), or other delegated protocol
  10. (`NSErrorRecoveryAttempting`) methods.
  11. A2BlockDelegate also supports replacing the delegate of a given class with an
  12. automatic - though optional - version of these block-based properties, while
  13. still allowing for traditional method-based delegate implementations.
  14. Simply call one of the methods in the category on a class to add a block
  15. property to that class, preferably during a `+load` method in a category.
  16. To interplay between classes that support delegation but are extended to have
  17. block properties through delegate replacement extended to have block
  18. properties, one should implement an `A2Dynamic<ProtocolName>` subclass of
  19. `A2DynamicDelegate`. This behavior is documented in detail in the class
  20. documentation for A2DynamicDelegate, and examples exist in BlocksKit.
  21. */
  22. @interface NSObject (A2BlockDelegate)
  23. /** @name Linking Block Properties */
  24. /** Synthesizes multiple properties and links them to the appropriate selector
  25. in the data source protocol.
  26. A2DynamicDelegate assumes a protocol name `FooBarDataSource` for instances of
  27. class `FooBar`. The method will generate appropriate `setHandler:` and
  28. `handler` implementations for each property name and selector name string pair.
  29. @param selectorsForPropertyNames A dictionary with property names as keys and
  30. selector strings as objects.
  31. */
  32. + (void)bk_linkDataSourceMethods:(NSDictionary *)selectorsForPropertyNames;
  33. /** Synthesizes multiple properties and links them to the appropriate selector
  34. in the delegate protocol.
  35. A2DynamicDelegate assumes a protocol name `FooBarDelegate` for instances of
  36. class `FooBar`. The method will generate appropriate `setHandler:` and
  37. `handler` implementations for each property name and selector name string pair.
  38. @param selectorsForPropertyNames A dictionary with property names as keys and
  39. selectors strings as objects.
  40. */
  41. + (void)bk_linkDelegateMethods:(NSDictionary *)selectorsForPropertyNames;
  42. /** Synthesizes multiple properties and links them to the appropriate selector
  43. in the given protocol.
  44. The method will generate appropriate `setHandler:` and `handler`
  45. implementations for each property name and selector name string pair.
  46. @param protocol A protocol that declares all of the given selectors. Must not
  47. be NULL.
  48. @param selectorsForPropertyNames A dictionary with property names as keys and
  49. selector strings as objects.
  50. */
  51. + (void)bk_linkProtocol:(Protocol *)protocol methods:(NSDictionary *)selectorsForPropertyNames;
  52. /** @name Delegate replacement properties */
  53. /** Registers a dynamic data source replacement using the property name
  54. `dataSource` and the protocol name `FooBarDataSource` for an instance of
  55. `FooBar`. */
  56. + (void)bk_registerDynamicDataSource;
  57. /** Registers a dynamic delegate replacement using the property name `delegate`
  58. and the protocol name `FooBarDelegate` for an instance of `FooBar`. */
  59. + (void)bk_registerDynamicDelegate;
  60. /** Registers a dynamic data source replacement using the given property name
  61. and the protocol name `FooBarDataSource` for an instance of `FooBar`.
  62. @param dataSourceName The name of the class' data source property. Must not be
  63. nil.
  64. */
  65. + (void)bk_registerDynamicDataSourceNamed:(NSString *)dataSourceName;
  66. /** Registers a dynamic delegate replacement using the given property name and
  67. the protocol name `FooBarDelegate` for an instance of `FooBar`.
  68. @param delegateName The name of the class' delegate property. Must not be nil.
  69. */
  70. + (void)bk_registerDynamicDelegateNamed:(NSString *)delegateName;
  71. /** Registers a dynamic protocol implementation replacement
  72. using the given property name and the given protocol.
  73. @param delegateName The name of the class' delegation protocol property, such
  74. as `safeDelegate`. Must not be nil.
  75. @param protocol A properly encoded protocol. Must not be NULL.
  76. */
  77. + (void)bk_registerDynamicDelegateNamed:(NSString *)delegateName forProtocol:(Protocol *)protocol;
  78. /** Creates or gets a dynamic delegate, assuring that it is the delegate.
  79. @see bk_dynamicDelegate:
  80. @return A dynamic delegate.
  81. */
  82. - (id)bk_ensuredDynamicDelegate;
  83. /** Creates or gets a dynamic protocol implementation, assuring that it is
  84. assigned to the delegate property correspending to that protocol
  85. @see bk_dynamicDelegateForProtocol:
  86. @return A dynamic delegate.
  87. */
  88. - (id)bk_ensuredDynamicDelegateForProtocol:(Protocol *)protocol;
  89. @end