2012-04-02 86 views
5

我正在開發一個應用程序,我必須在我的應用程序的所有頁面中顯示iAds .. 我創建了UIView的子類,其中我正在初始化ADBannerView及其委託方法。如何讓iAd在我的應用程序中全局查看?

但現在,如果我將其添加在窗口AppDelegate類它給我以下運行時錯誤 「ADBannerView必須是一個UIViewController管理視圖層次的一部分」 ..

我想,這是不是意味着我只能在UIViewController的子類文件中使用ADBanner?

如果是這樣的話我該如何讓它成爲全球?

由於提前 SHREYA

+1

也請看看接受的答案在這裏:http://stackoverflow.com/questions/9422177/is-it-a-good-practice-to-delete-the-adbannerview-on-viewwilldisappear-and-add-它/ 9422360#9422360 – 2012-04-02 09:04:44

+0

嘿@ user1036925:如果你從下面的帖子得到答案。請接受它。 :) – 2012-04-02 09:39:58

+0

@shreya:把它放在appdelegate的窗口上,然後它可能.....! – 2012-10-16 17:13:36

回答

6

在AppDelegate類中,你可以做一個共享對象。

- (ADBannerView *) sharedBannerView 
{ 
    if (_sharedBannerView == nil) 
    { 
     Class classAdBannerView = NSClassFromString(@"ADBannerView"); 

     if (classAdBannerView != nil) 
     { 
      _sharedBannerView = [[classAdBannerView alloc] initWithFrame:CGRectMake(0, 480, 320, 50)]; 

      // pre 4.2 doesn't have the new AdBannerSize constants. 
      if (&ADBannerContentSizeIdentifierPortrait != NULL) 
      { 
       [_sharedBannerView setRequiredContentSizeIdentifiers:[NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil]];    
      } 
      else 
      { 
       [_sharedBannerView setRequiredContentSizeIdentifiers:[NSSet setWithObjects:ADBannerContentSizeIdentifier320x50, ADBannerContentSizeIdentifier480x32, nil]];    
      } 
     } 
    } 

    ((ADBannerView *)_sharedBannerView).backgroundColor = [UIColor whiteColor]; 

    return _sharedBannerView; 
} 

並將此共享對象添加到任何需要顯示iAd的視圖中。 希望你得到它。

+0

嗨Neelam。 thanx爲您的答覆。我會執行它肯定.. – Shreya 2012-04-02 10:31:23

+0

@ user1036925:也看看這個蘋果技術說明:[TN2286:實施共享的iAd橫幅](https://developer.apple.com/library/ios/#technotes/ tn2286/_index.html#// apple_ref/DOC/UID/DTS40011212) – 2012-04-19 11:00:30

相關問題