我已经将一个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的引用?
答案 0 :(得分:1)
#import "AppDelegate.h"
// ...
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];