Sadiq Jaffer
Sadiq Jaffer

Reputation: 500

Objective-C delegate function not working correctly

I have an app that has three views. All three views have an Ad Banner at the bottom of the screen. The first view creates an audio streamer which is paused when the Ad Banner on this view is clicked. I'm trying to use the AdBanner delegate methods on the second view to stop/start the audio. When the Ad Banner is selected the AdBanner delegate methods should call my custom delegate functions. The code compiles and runs but doesn't function correctly.

Using NSLog I've determined that the Ad Banner is calling its delegate function correctly but this isn't calling the custom delegate.

Hope this makes sense. Any help would be appreciated. Here is my code.

SecondViewControler H-file

#import "SecondViewController.h"

@protocol demoViewControllerDelegate <NSObject>
@required

-(void)stopSent;
-(void)startSent;

@end

@interface SecondViewController ()
{    
    id<demoViewControllerDelegate> delegate;
}    
@property (nonatomic, assign) id<demoViewControllerDelegate> delegate;
@end

SecondViewController M-file

@implementation SecondViewController
@synthesize delegate;

Protocols

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
    [delegate stopSent];
    return YES;
}

- (void)bannerViewActionDidFinish:(ADBannerView *)banner{
[delegate startSent];
}

FirstViewController H-file

#import <QuartzCore/QuartzCore.h>
#import "iAd/iAd.h"
#import <MessageUI/MessageUI.h>
#import "AudioStreamer.h"
#import "Reachability.h"
#import "SecondViewController.h"
#import "MFAppDelegate.h"
#import "MFSideMenu.h"

Class secondViewConroller;

@interface DemoViewController : UIViewController <ADBannerViewDelegate,demoViewControllerDelegate> {
}

@end

FirstViewController M-file

-(void)stopSent{
    if (isPlaying) {
        [streamer stop];
        wasPlaying=true;
    }
}

-(void)startSent{
    if (wasPlaying) {
        [streamer start];
         isPlaying=true;
    }
 }

Upvotes: 0

Views: 462

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89509

Your protocol methods need to be implemented in the class that you've designated as your delegate target.

It looks like your DemoViewController (or FirstViewController) is the object you've designated as the delegate, since you've given the interface the "<ADBannerViewDelegate,demoViewControllerDelegate>" designations.

Then, from your Second View Controller, you can call the object you designated and set as a delegate by doing:

[delegate startSent];

and

[delegate stopSent];

in the appropriate locations, which appear to be "bannerViewActionShouldBegin" and "bannerViewActionDidFinish", respectively.

You should also make sure that the delegate is properly set, therefore instead of:

[delegate startSent];

you should actually do this:

if(delegate)
    [delegate startSent];
else
    NSLog( @"delegate is null; we should figure out why" );

Upvotes: 2

Related Questions