2012-10-25 77 views
1

我想在我的应用中添加AdMob横幅! 我得到它在UIViewcontrollers工作,但不是在我的UIView ...横幅显示,但当你点击它,没有反应!如果你点击UIViewcontrollers中的横幅,它就可以工作!AdMob广告未出现在UIView中iPhone

守则UIViewcontrollers(工作!)

- (void)viewDidLoad 
{ 

[super viewDidLoad]; 
bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height - GAD_SIZE_320x50.height-49, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height)]; 

bannerView_.adUnitID = MY_BANNER_UNIT_ID; 
bannerView_.rootViewController = self; 
[self.view addSubview:bannerView_]; 


GADRequest *r = [[GADRequest alloc] init]; 
r.testing = YES; 
[bannerView_ loadRequest:r]; 
// Do any additional setup after loading the view, typically from a nib. 

}

代码中的UIView:(不工作)

- (void)drawRect:(CGRect)rect 
    { 




    // Create a view of the standard size at the bottom of the screen. 
    bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0, self.frame.size.height - GAD_SIZE_320x50.height, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height)]; 
    // Specify the ad's "unit identifier." This is your AdMob Publisher ID. 
    bannerView_.adUnitID = MY_BANNER_UNIT_ID; 

    // Let the runtime know which UIViewController to restore after taking 
    // the user wherever the ad goes and add it to the view hierarchy. 
    bannerView_.rootViewController = self; 
[self addSubview:bannerView_]; 

    // Initiate a generic request to load it with an ad. 
    [bannerView_ loadRequest:[GADRequest request]]; 
    // Drawing code 
    } 

行bannerView_.rootViewController =自我;给我一个警告,但没有它,它就无法工作!警告说“不兼容的指针类型从‘blandad * const_strong’

blandad的名称为OM了UIView的的.m和.h文件分配给‘UIViezController *’!

你觉得有什么不对?

/A小白

+0

http://stackoverflow.com/questions/18043931/set-admob-banner-to-appear-at-bottom-of-screen/18044011#18044011 – Guru

回答

2

请从- (void)drawRect:(CGRect)rect方法删除所有的代码,你必须要在图形上下文中绘制的东西的情况下,以覆盖此方法。这种方法将被调用不是一次,而是很多次,所以每次你将分配一个新的视图并将其添加为子视图

如果您想将广告横幅添加到自定义的UIView的情况下,就做这样的事情:

// in your CustomView implementation 
- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 

     bannerView_ = [[GADBannerView alloc] initWithFrame:CGRectMake(0.0, self.view.frame.size.height - GAD_SIZE_320x50.height-49, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height)]; 

     bannerView_.adUnitID = MY_BANNER_UNIT_ID; 

     UIViewController* root = [(YOUR_APP_DELEGATE_CLASS*)[[UIApplication sharedApplication] delegate] viewController]; 
     bannerView_.rootViewController = root; 
     [self addSubview:bannerView_]; 


     GADRequest *r = [[GADRequest alloc] init]; 
     r.testing = YES; 
     [bannerView_ loadRequest:r]; 

    } 
    return self; 
} 

您可以设置UIViewController* root与你有任何控制器,最好是设置有一个控制器哪个视图是您自定义视图的父视图。我刚刚从应用程序委托中为根视图控制器写了一个路径,如果您想使用它,请确保您的应用程序代理具有此viewController属性。

+0

好!所以我把它放在哪里? – Mangy92

+0

只是删除它,因为你已经在'viewDIdLoad'中,并且正如我所看到的,你做的一切都是正确的,它不是很好吗?你能再描述一个问题吗?目标是什么? – Ezeki

+0

这是两种不同的看法!第一个工作,因为它在UIViewControllers ..但第二个不能正常工作,因为它是一个UIView! – Mangy92

0

我不知道你是否解决了你的问题,但从我的部分我得到了同样的问题。

将GAD横幅视图添加到继承自UIView的适当视图,然后横幅显示得很好,但Admob链接未处于活动状态。

在我的情况下,GAD横幅视图被添加到GADBannerView的adViewDidReceiveAd委托中。显然这不是好的方法。

通过直接在初始化后添加GAD横幅视图,现在一切正常。

- (void)initGADBannerViewWithId:(NSString *)bannerId andRootController:(UIViewController *)rootController 
{ 
    _gadBannerView = [[GADBannerView alloc] initWithFrame:self.frame]; 

    _gadBannerView.adUnitID = bannerId; 

    // Let the runtime know which UIViewController to restore after taking 
    // the user wherever the ad goes and add it to the view hierarchy. 
    _gadBannerView.rootViewController = rootController; 
    _gadBannerView.delegate = self; 
    [self addSubview:_gadBannerView]; 
} 

而在GADBannerView的委托adViewDidReceiveAd,现在我只是有一个动画修改横幅的位置有一个动态的视觉效果。

希望这可以帮助别人