Inherits from NSProxy
Declared in A2DynamicDelegate.h

Overview

A2DynamicDelegate implements a class’s delegate, data source, or other delegated protocol by associating protocol methods with a block implementation.

- (IBAction) annoyUser
{
    // Create an alert view
    UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Hello World!"
                              message:@"This alert's delegate is implemented using blocks. That's so cool!"
                              delegate:nil
                              cancelButtonTitle:@"Meh."
                              otherButtonTitles:@"Woo!", nil];

    // Get the dynamic delegate
    A2DynamicDelegate *dd = alertView.bk_dynamicDelegate;

    // Implement -alertViewShouldEnableFirstOtherButton:
    [dd implementMethod:@selector(alertViewShouldEnableFirstOtherButton:) withBlock:^(UIAlertView *alertView) {
        NSLog(@"Message: %@", alertView.message);
        return YES;
    }];

    // Implement -alertView:willDismissWithButtonIndex:
    [dd implementMethod:@selector(alertView:willDismissWithButtonIndex:) withBlock:^(UIAlertView *alertView, NSInteger buttonIndex) {
        NSLog(@"You pushed button #%d (%@)", buttonIndex, [alertView buttonTitleAtIndex:buttonIndex]);
    }];

    // Set the delegate
    alertView.delegate = dd;

    [alertView show];
}

A2DynamicDelegate is designed to be ‘plug and play’.

Tasks

Other Methods

  • – initWithProtocol:

    The designated initializer for the A2DynamicDelegate proxy.

  •   protocol

    The protocol delegating the dynamic delegate.

    property
  •   handlers

    A dictionary of custom handlers to be used by custom responders in a A2Dynamic(Protocol Name) subclass of A2DynamicDelegate, like A2DynamicUIAlertViewDelegate.

    property
  •   realDelegate

    When replacing the delegate using the A2BlockDelegate extensions, the object responding to classical delegate method implementations.

    property

Block Instance Method Implementations

Block Class Method Implementations

Properties

handlers

@property (nonatomic, strong, readonly) NSMutableDictionary *handlers
Discussion

A dictionary of custom handlers to be used by custom responders in a A2Dynamic(Protocol Name) subclass of A2DynamicDelegate, like A2DynamicUIAlertViewDelegate.

Declared In

A2DynamicDelegate.h

protocol

@property (nonatomic, readonly) Protocol *protocol
Discussion

The protocol delegating the dynamic delegate.

Declared In

A2DynamicDelegate.h

realDelegate

@property (nonatomic, weak, readonly) id realDelegate
Discussion

When replacing the delegate using the A2BlockDelegate extensions, the object responding to classical delegate method implementations.

Declared In

A2DynamicDelegate.h

Instance Methods

blockImplementationForClassMethod:

- (id)blockImplementationForClassMethod:(SEL)selector
Discussion

The block that is to be fired when the specified selector is called on the delegating object’s class.

Parameters

selector

An encoded selector. Must not be NULL.

Return Value

A code block, or nil if no block is assigned.

Declared In

A2DynamicDelegate.h

blockImplementationForMethod:

- (id)blockImplementationForMethod:(SEL)selector
Discussion

The block that is to be fired when the specified selector is called on the reciever.

Parameters

selector

An encoded selector. Must not be NULL.

Return Value

A code block, or nil if no block is assigned.

Declared In

A2DynamicDelegate.h

implementClassMethod:withBlock:

- (void)implementClassMethod:(SEL)selector withBlock:(id)block
Discussion

Assigns the given block to be fired when the specified selector is called on the reciever.

Warning: Starting with A2DynamicDelegate 2.0, a block will not be checked for a matching signature. A block can have less parameters than the original selector and will be ignored, but cannot have more.

Parameters

selector

An encoded selector. Must not be NULL.

block

A code block with the same signature as selector.

Declared In

A2DynamicDelegate.h

implementMethod:withBlock:

- (void)implementMethod:(SEL)selector withBlock:(id)block
Discussion

Assigns the given block to be fired when the specified selector is called on the reciever.

[tableView.dynamicDataSource implementMethod:@selector(numberOfSectionsInTableView:)
                                  withBlock:NSInteger^(UITableView *tableView) {
    return 2;
}];

Warning: Starting with A2DynamicDelegate 2.0, a block will not be checked for a matching signature. A block can have less parameters than the original selector and will be ignored, but cannot have more.

Parameters

selector

An encoded selector. Must not be NULL.

block

A code block with the same signature as selector.

Declared In

A2DynamicDelegate.h

initWithProtocol:

- (id)initWithProtocol:(Protocol *)protocol
Discussion

The designated initializer for the A2DynamicDelegate proxy.

An instance of A2DynamicDelegate should generally not be created by the user, but instead by calling a method in NSObject(A2DynamicDelegate). Since delegates are usually weak references on the part of the delegating object, a dynamic delegate would be deallocated immediately after its declaring scope ends. NSObject(A2DynamicDelegate) creates a strong reference.

Parameters

protocol

A protocol to which the dynamic delegate should conform.

Return Value

An initialized delegate proxy.

Declared In

A2DynamicDelegate.h

removeBlockImplementationForClassMethod:

- (void)removeBlockImplementationForClassMethod:(SEL)selector
Discussion

Disassociates any blocks so that nothing will be fired when the specified selector is called on the delegating object’s class.

Parameters

selector

An encoded selector. Must not be NULL.

Declared In

A2DynamicDelegate.h

removeBlockImplementationForMethod:

- (void)removeBlockImplementationForMethod:(SEL)selector
Discussion

Disassociates any block so that nothing will be fired when the specified selector is called on the reciever.

Parameters

selector

An encoded selector. Must not be NULL.

Declared In

A2DynamicDelegate.h