【问题标题】:How to create a global reference for iAd and implement in multiple Viewcontrollers如何为 iAd 创建全局引用并在多个 Viewcontroller 中实现
【发布时间】:2012-05-17 00:01:33
【问题描述】:

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

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

【问题讨论】:

    标签: objective-c xcode ios5 iad


    【解决方案1】:

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

    #import "AppDelegate.h" 
    

    如果没有网络连接或添加不显示,你不应该隐藏添加

    【讨论】:

      【解决方案2】:

      在 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,并在要显示 iAd 的 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 示例。原始 layoutAnimated 函数可以在 iAdSuite 示例中找到。不要忘记将 iAdSuite 示例中的委托函数添加到您的视图控制器中。

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-09-27
          • 1970-01-01
          • 1970-01-01
          • 2021-08-25
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多