如何为 iAd 创建全局引用并在多个视图控制器中实现


我有 5 个视图控制器,每个控制器都必须

显示 iAd,因此我必须在每个视图控制器中实现 iAd 代码。相反,如果我在 AppDelegate 中创建一组通用代码,则意味着我可以在需要显示 iAd 的任何地方调用该代码。

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

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

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

然后在您的视图控制器中执行以下操作:

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

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

嗨,

这看起来是一个很好的答案,但您不必在 m 文件中导入 AppDelegate

#import "AppDelegate.h" 

并且您不应该隐藏如果没有网络连接或添加未显示的添加

在 AppDelegate.h 中:

#import <iAd/iAd.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    ADBannerView *_bannerView;
...
...
}
@property (nonatomic, retain) ADBannerView *_bannerView;

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

在需要添加 iAd 的视图控制器中,创建名为"vwAd"的容器 UIView,并在要显示 iAds 的 xib 文件中建立连接。

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

另请查看 Apple 的 iAdSuite 示例。原始布局动画功能可以在iAdSuite示例中找到。不要忘记将 iAdSuite 示例中的委托函数添加到视图控制器中。

最新更新