2012-05-08 46 views
6

Mam 5 ViewControllers w każdym z nich muszę wyświetlić iAd, więc muszę wdrożyć kod iAd w każdym ViewControllers. Zamiast tego, jeśli utworzyć zestaw wspólnego kodu w AppDelegate oznacza to, że mogę po prostu wywołać ten kod wszędzie tam, gdzie potrzebuję iAd do wyświetlenia.Jak utworzyć globalną referencję dla iAd i implementacji w wielu kontrolerach ViewControllers

Jeśli ktoś wdrożył tę koncepcję iAd, pomóż mi wydostać się z tego problemu. Z góry dziękuję.

Odpowiedz

5

Wystarczy utworzyć wskaźnik do iAD w delegacie aplikacji.

.h:

@property (strong, nonatomic) ADBannerView *UIiAD; 

.m:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    UIiAD = [[ADBannerView alloc] init]; 
    return YES; 
} 

Następnie w swoim ViewControllers to zrobić:

.h:

@property (strong, nonatomic) ADBannerView *UIiAD; 

.M:

- (AppDelegate *) appdelegate { 
    return (AppDelegate *)[[UIApplication sharedApplication] delegate]; 
} 

- (void) viewWillAppear:(BOOL)animated { 
    UIiAD = [[self appdelegate] UIiAD]; 
    UIiAD.delegate=self; 
    // CODE to correct the layout 
} 

- (void) viewWillDisappear:(BOOL)animated{ 
    UIiAD.delegate=nil; 
    UIiAD=nil; 
    [UIiAD removeFromSuperview]; 
} 

Zrób to dla wszystkich kontrolerów widoku z odpowiednim kodem, aby zmienić wygląd układu!

1

hi to wygląda dobra odpowiedź, ale nie masz do importowania AppDelegate w plikach m

#import "AppDelegate.h" 

i nie należy ukryć dodać jeśli nie ma połączenia z siecią lub nie dodawać pokazując

0

W AppDelegate.h:

#import <iAd/iAd.h> 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 
{ 
    ADBannerView *_bannerView; 
... 
... 
} 

@property (nonatomic, retain) ADBannerView *_bannerView; 

W AppDelegate.m:

@synthesize _bannerView; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
... 
    if ([ADBannerView instancesRespondToSelector:@selector(initWithAdType:)]) { 
     self._bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner]; 
    } else { 
     self._bannerView = [[ADBannerView alloc] init]; 
    } 
... 
} 

W kontrolkach widoku, które trzeba dodać iAd, utwórz kontener UIView o nazwie "vwAd" i utwórz połączenie w pliku XIB, w którym chcesz wyświetlić iAds.

@interface ViewController : UIViewController<ADBannerViewDelegate> 
{ 
    IBOutlet UIView *vwAd; 
... 
... 
} 

- (void)layoutAnimated:(BOOL)animated 
{ 
    CGRect contentFrame = self.view.bounds; 
    if (contentFrame.size.width < contentFrame.size.height) { 
     self.appDelegate._bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait; 
    } else { 
     self.appDelegate._bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape; 
    } 

    CGRect bannerFrame = self.appDelegate._bannerView.frame; 
    if (self.appDelegate._bannerView.bannerLoaded) { 
     bannerFrame.origin.y = 0; 
    } else { 
     bannerFrame.origin.y = vwAd.frame.size.height; 
    } 

    [UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{ 
     self.appDelegate._bannerView.frame = bannerFrame; 
    }]; 
} 


- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:animated]; 
    ... 

    [self.appDelegate._bannerView removeFromSuperview]; 
    self.appDelegate._bannerView.delegate = nil; 
    self.appDelegate._bannerView.delegate = self; 
    [vwAd addSubview:self.appDelegate._bannerView]; 
    [self layoutAnimated:NO]; 
} 

Proszę również zapoznać się z przykładami iAdSuite od Apple. Oryginalny układ funkcji Animated można znaleźć w przykładach iAdSuite. Nie zapomnij dodać funkcji delegata z przykładu iAdSuite do kontrolera widoku.

Powiązane problemy