如何为iAd创建全局引用并在多个Viewcontrollers中实现

Sud*_*son 6 xcode objective-c iad ios5

我每个都有5个ViewControllers,我必须显示iAd,所以我必须在每个ViewControllers中实现iAd代码.而不是如果我在AppDelegate中创建一组公共代码意味着我可以在任何需要iAd显示的地方调用该代码.

如果有人实施了这个iAd概念意味着帮助我摆脱这个问题.提前致谢.

Joã*_*nes 5

只需在APP委托中创建指向iAD的指针即可.

.H:

@property (strong, nonatomic) ADBannerView *UIiAD;
Run Code Online (Sandbox Code Playgroud)

.M:

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

然后在ViewControllers中执行以下操作:

.H:

@property (strong, nonatomic) ADBannerView *UIiAD;
Run Code Online (Sandbox Code Playgroud)

.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];
}
Run Code Online (Sandbox Code Playgroud)

使用适当的代码为所有视图控制器执行此操作以重新设计布局!