繁体   English   中英

如何将AppDelegate的引用发送到Viewcontroller子类?

[英]How to send reference of AppDelegate to Viewcontroller subclass?

我像这样子化了一个viewController类。

.h文件

#import "GADBannerView.h"

@interface ViewController : SPViewController
{
    GADBannerView *bannerView;
}

@property GADBannerView *bannerView;

@end

.m文件

#import "ViewController.h"
#import "GADBannerView.h"
#import "GADRequest.h"

@implementation ViewController

@synthesize bannerView;

- (void)viewDidLoad {

    [super viewDidLoad];
    // Do any additional setup after loading the view.

    bannerView = [[GADBannerView alloc] initWithFrame:CGRectMake(0,0, 320, 50)];

    // Replace this ad unit ID with your own ad unit ID.
    self.bannerView.adUnitID = @"ca-app-pub-LONG ID HERE";
    self.bannerView.rootViewController = self;

    [self.view addSubview:bannerView];

    GADRequest *request = [GADRequest request];

    // Requests test ads on devices you specify. Your test device ID is printed to the console when
    // an ad request is made.
    request.testDevices = @[ GAD_SIMULATOR_ID, @"SIM ID HERE" ];
    [self.bannerView loadRequest:request];


}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

在我的AppDelegate中,

 _viewController = [[ViewController alloc] init];


    [_viewController startWithRoot:[Game class] supportHighResolutions:YES doubleOnPad:YES];

    [_window setRootViewController:_viewController];
    [_window makeKeyAndVisible];

一切正常,但是我需要能够在ViewController子类中添加一个开关,以决定是否设置AdMob广告,将在plist中设置的布尔值,该布尔值可以通过AppDelegate访问,所以我需要将AppDelegate的引用发送到我的子类ViewController,因此我可以执行以下操作

if ([[appDelegate.coreData objectForKey:@"AdMob"] boolValue] == YES) {
       //Admob setup here
    }

在我的ViewController子类中。 如何在ViewController子类中获取对AppDelegate的引用?

#import "AppDelegate.h"
// ...
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM