2014-12-28 53 views
0

我一個子類的viewController類,像這樣...如何將AppDelegate的引用發送給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廣告是否設置好,這是一個布爾值,它將設置在通過AppDelegate訪問的plist中,所以我需要將AppDelegate的引用發送到我的子類ViewController,以便我可以做像

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

在我的ViewController子類。我怎樣才能得到我的ViewController子類中的AppDelegate的引用?

回答

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

很酷,如果我想通過我的ViewController子類發送對另一個類的引用呢? – Phil

+0

這取決於什麼類以及它的對象是如何創建的。 ('sharedApplication'是一個單例對象,所以它可以在任何地方使用。) –

+0

但是我不明白的是,當你實例化一個類(僅僅是一個標準類)的時候,如何向我的ViewController子類發送一個引用,因爲它在父類上調用Init,所以我不知道如何通過子類獲取某些內容。 – Phil

相關問題