【发布时间】:2014-12-28 13:38:39
【问题描述】:
我已经继承了一个 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 的引用?
【问题讨论】:
标签: ios objective-c xcode viewcontroller appdelegate