Showing posts with label delegate. Show all posts
Showing posts with label delegate. Show all posts

Thursday, July 23, 2015

Creating an object with multiple delegates in iOS 8

Sometimes one delegate is just not enough and notifications are just not reliable or their timing is to unpredictable. Here comes the solution, multiple delegates! Bonus, this should be multithreading safe.

The multiple delegates still will not give you an order on which receiver gets the information first but if you want that, you aren't programming correctly.

First you need to make a delegate protocol;

@protocol AudioPlayerDelegate <NSObject>
@optional
- (void)AudioPlayer:(AudioPlayer *)audioEngine onStateChange:(StreamStatusType)state;

@end

in the interface, create the delegate calls;

//Delegates
- (void)addDelegate:(id<AudioPlayerDelegate>)aDelegate;
- (void)removeDelegate:(id<AudioPlayerDelegate>)aDelegate;

Also in the interface create a private delegate as a hashtable;

@private
  NSHashTable *delegates;

in the implementation, setup the delegates in the init;

  delegates = [NSHashTable weakObjectsHashTable];

and create the methods;

#pragma mark - Delegates

- (void)addDelegate:(id<AudioPlayerDelegate>)aDelegate {
    [delegates addObject:aDelegate];
}

- (void)removeDelegate:(id<AudioPlayerDelegate>)aDelegate {
    [delegates removeObject:aDelegate];
}

when you want to use them, here is the best and safest way to do so;

    __weak AudioPlayer *weakSelf = self;
    id<AudioPlayerDelegate> delegate = nil;
    for (delegate in [delegates copy]) {
        if (delegate && [delegate respondsToSelector:@selector(AudioPlayer:onStateChange:)]) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [delegate AudioPlayer:weakSelf onStateChange:weakSelf.streamStatusType];
            });
       }
    }

This might be overkill but it is always good to be thread safe. 

And that's it! 

Thursday, May 2, 2013

iOS Creating Custom Delegates in iOS6 with ARC

So you want two Objects to talk to each other more than a little bit? Here is what to do by example.  Lets say we have our MainViewController and a new SearchViewController that you want a delegate attached to it. This is all you need to do.

In SearchViewController.h
@protocol SearchViewDelegate <NSObject>
@optional
@required
-(void)didAskToSearch:(NSString*)string;
@end

@interface SearchViewController : UIViewController {
}
@property (nonatomic, assign) id <SearchViewDelegate> delegate;
@end

optional has non-required methods and required of course has required methods. 

In SearchViewController.m;
    [_delegate didAskToSearch: [self searchRequestDetails]];

this is put anywhere in the code when you want it to send the information back to the other object. 

In the MainViewController.h;
@interface MainViewController : UIViewController <SearchViewDelegate> {
}

In MainViewController.m;
SearchViewController *searcher = [[SearchViewController alloc]init];
searcher.delegate = self;

#pragma mark - SearchViewControllerDelegates
-(void)didAskToSearch:(NSString*)string{
     //do whatever you want with the string results
}

When creating the object, make sure the tell the delegate you have those methods. Then you just have to create the methods as per it is required. 

Helpful link
http://stackoverflow.com/questions/12020539/how-to-make-custom-delegate-in-ios-app