2012-05-08 5 views
6

Tengo 5 ViewControllers en cada uno de los que tengo para mostrar iAd, por lo que tengo que implementar el código iAd en cada ViewControllers. En lugar de eso, si creo un conjunto de código común en AppDelegate, significa que puedo simplemente llamar a ese código donde sea que necesite que se muestre iAd.Cómo crear una referencia global para iAd e implementar en múltiples Viewcontrollers

Si alguien ha implementado este concepto iAd significa que me ayuda a salir de este problema. Gracias por adelantado.

Respuesta

5

Simplemente cree un puntero a iAD en el delegado de APP.

.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; 
} 

Luego, en sus ViewControllers hacer esto:

.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]; 
} 

¡Haga esto para todos los controladores de vista con el código apropiado para rediseñar el diseño!

1

hi esto parece una buena respuesta, pero no hay que importar el AppDelegate en sus archivos m

#import "AppDelegate.h" 

y ¿no debería ocultar el add si no hay conexión a la red o no añadir mostrando

0

En el AppDelegate.h:

#import <iAd/iAd.h> 

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

@property (nonatomic, retain) ADBannerView *_bannerView; 

En el 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]; 
    } 
... 
} 

En los controladores de vista que necesita para agregar iAd, cree un contenedor UIView con el nombre 'vwAd' y realice la conexión en el archivo xib donde desee mostrar 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]; 
} 

También revise ejemplos de iAdSuite de Apple. Diseño original La función animada se puede encontrar en ejemplos de iAdSuite. No olvide agregar las funciones de delegado del ejemplo de iAdSuite en su controlador de vista.

Cuestiones relacionadas