1

I have tried using a singleton class in my app delegate but I haven't been able to get that to work. I've also checked out the iAdSuite examples (particularly the containerBanner example because it seemed to be the most relative) but I can't figure it out. If there's a better way to accomplish this without using a singleton class and you can point me in the right direction I'd really appreciate it. Some of my singleton class code is below. Thank you!

@interface App Delegate

@property (assign) iAdController *iadc;
+ (AppDelegate*) sharedApplication;
- (iAdController*)sharedAd;
@end

@implementation AppDelegate

@synthesize iadc;

+ (AppDelegate*) sharedApplication
{
return [[UIApplication sharedApplication] delegate];
}

-(iAdController*)sharedAd
{
    if(iadc==nil){
        iadc=[iAdController new];
    }
    return iadc;
}


@interface ViewController

iAdController*iadc=[[AppDelegate sharedApplication] sharedAd];
//here i get an error saying, "initializer element is not a compile-time constant.

Everything is imported correctly. If there's anything else I should post let me know.

Daniel
  • 23,129
  • 12
  • 109
  • 154
Dave123
  • 306
  • 2
  • 4
  • 13

1 Answers1

0

try changing your singleton creation to this:

+ (LocationManagerSingleton*)sharedInstance {

    static LocationManagerSingleton *_sharedInstance;
    if(!_sharedInstance) {
        static dispatch_once_t oncePredicate;
        dispatch_once(&oncePredicate, ^{
            _sharedInstance = [[super allocWithZone:nil] init];
        });
    }

    return _sharedInstance;
}



+ (id)allocWithZone:(NSZone *)zone {    

    return [self sharedInstance];
}


- (id)copyWithZone:(NSZone *)zone {
    return self;    
}

- (id)init
{
    self = [super init];
    if (self != nil) 
    {
        // PERFORM any custom initialization here
    }
    return self;
}

Obviously change the name of the class.

Whenever you want to use your singleton in any of your viewcontrollers just call it like this:

locationManager = [LocationManagerSingleton sharedInstance];

Dont forget to add

+ (LocationManagerSingleton*) sharedInstance;

on the header.

EDIT

well it seems i misunderstood your code (forget my answer, you simply want to be able to access your iAdController from everywhere. so just place

Add inside the .m of the ViewController

@interface ViewController()
{
    iAdController *iadc; 
}

And inside the

-(void)viewDidLoad
{
    iadc=[[AppDelegate sharedApplication] sharedAd];
}

but import the app delegate.h on whichever viewcontroller you want to use it in.

#import "AppDelegate.h" 

also there shouldnt be a space in the AppDelegate on the @interface

Pochi
  • 13,391
  • 3
  • 64
  • 104
  • I believe that your edit is what I tried initially and it gives me an error saying, "initializer element is not a compile-time constant". Any idea how to fix this and make it work? Is there another way to share a single instance of an iAdbanner throughout viewcontrollers that would be easier to implement? Thanks for the help btw :) – Dave123 Aug 10 '12 at 06:30
  • the important thing about the edit is that it should be placed in your viewdidload or wherever you want to use it inside the viewcontroller that you want to use it at. you cannot put it under @interface like that. for example in your viewcontroller.m file there must be a method called viewdidload. put it there and it will work. If you want to make it available for the whole class you have to place the pointer within the instance variables and initialize it in the viewdidload – Pochi Aug 10 '12 at 06:58
  • Ok, I made the changes and the code runs now without pausing. But now it's saying that "iadc" is an unused variable within my ViewDidLoad. And I'm still not able to get the ad banner to appear in the simulator. Pretty frustrating. – Dave123 Aug 10 '12 at 07:24
  • because you just got the reference to the instance of the iad i dont know how you want to display it. I have never used adds to be honest. You should read the documentation for them. – Pochi Aug 10 '12 at 07:39
  • I've used the code within my iAdController class before for displaying adBanners from the iAd network before so I know that works. I've read the documentation for shared banners and it says to create an ADBannerView within the App Delegate and then use the App delegate as the banner's delegate. I'm pretty new to ios development so I'm having a difficult time doing that. I thought a singleton class might work as well and be easier to implement. thanks for the help, I appreciate it. – Dave123 Aug 10 '12 at 07:47