How to make a single shared instance of iAd banner throughout many view controllers?

◇◆丶佛笑我妖孽 提交于 2019-12-06 15:11:42

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!