我每个都有5个ViewControllers,我必须显示iAd,所以我必须在每个ViewControllers中实现iAd代码。而不是如果我在AppDelegate中创建公共代码集意味着我可以只在我需要iAd显示的地方调用该代码。
如果有人实施了这个iAd概念意味着帮助我摆脱这个问题。在此先感谢。
答案 0 :(得分:5)
只需在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;
}
然后在ViewControllers中执行以下操作:
·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];
}
为所有视图控制器执行此操作,并使用适当的代码重新设计布局!
答案 1 :(得分:1)
嗨,这看起来很好,但你不必在你的m文件中导入AppDelegate
#import "AppDelegate.h"
如果没有网络连接或添加不显示
,则不应隐藏添加答案 2 :(得分:0)
在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,并在xib文件中建立要显示iAd的连接。
@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示例。原始layoutAnimated函数可以在iAdSuite示例中找到。不要忘记将iAdSuite示例中的委托函数添加到视图控制器中。