0

I've been reading all evening and it seems that to get an adWhirl ad use the same add banner across all view controllers, I need to create a singleton awView and use this in the ViewWillLoad & ViewWillUnload of each View.

I am having trouble getting this to work. I've found tons of AdWhirl tutorials but none that create a singleton.

I currently have this adWhirlSingleton.h

#import <Foundation/Foundation.h>
#import "AdWhirlDelegateProtocol.h"
#import "AdWhirlView.h"

@interface adWhirlSingleton : NSObject <AdWhirlDelegate> {
    AdWhirlView *awView;
    UIViewController *primaryView;
}

@property (strong, nonatomic) AdWhirlView *awView;
@property (strong, nonatomic) UIViewController *primaryView;

@end

adWhirlSingleton.m

#import "adWhirlSingleton.h"

@implementation adWhirlSingleton
@synthesize primaryView, awView;

-(NSString *)adWhirlApplicationKey
{
    return @"my key here";
}

-(UIViewController *)viewControllerForPresentingModalView
{
    return primaryView;
}

@end

I import adWhirlSingleton into my views, but when I type adWhirlSingleton.primaryView = self I doesn't recognize the primaryView.

What am I missing to implement this? Thanks

Darren
  • 10,182
  • 20
  • 95
  • 162

1 Answers1

1

Singletons have a factory init method (the method will start with a + instead of a -) to ensure that only one gets created. You may have other things going on, but any object that doesn't make itself a singleton won't be a singleton.

Here is a stack overflow question about creating singletons that might help. This one will also give examples There is also an excellent Matt Gallagher post about them. Once you create the singleton instance you will always reference it with something like
[[adWhirlSingleton sharedSingleton] primaryView]
If you've just been starting out, the Application Delegate is a singleton, so if you see demo code that uses the Application Delegate and its shared instance, you will have some examples for how to reference a singleton.

Community
  • 1
  • 1
Walter
  • 5,867
  • 2
  • 30
  • 43
  • Thanks Walter. That was some good links that explained a lot about Singletons. Got it working now :-) – Darren Jan 02 '12 at 09:56