本文介绍了Monotouch.Dialog和iAds的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用iAds.我想将其添加到基本上是一堆Monotouch.Dialog视图的应用程序中.

I'm playing with iAds. I want to add it to an app that is basically a bunch of Monotouch.Dialog views.

最佳做法是添加 UIViewController ,然后向其中添加 ADBannerView Monotouch.Dialog ,或者我应该添加 iAds 视图访问 Monotouch.Dialog ViewController?

Is the best practise to add a UIViewController, then add a ADBannerView and a Monotouch.Dialog to it , or should I add the iAds view to the Monotouch.Dialog ViewController?

推荐答案

我创建了一个效果很好的包装ViewController,只需像TabBar或SplitView等那样传入主应用程序的视图,然后将IADViewController用作RootViewCOntroller:

I created a wrapper ViewController that works nicely, just pass in the main app's view like a TabBar or SplitView etc and use IADViewController as the RootViewCOntroller:

     public partial class IADViewController : UIViewController
{
    private UIViewController _anyVC;
    private MonoTouch.iAd.ADBannerView _ad;

    public IADViewController (UIViewController anyVC)
    {
        _anyVC = anyVC;
    }

    public override void ViewDidLoad ()
    {
        base.ViewDidLoad ();

        View.AddSubview (_anyVC.View);


        if (Common.Device.Is6AtLeast && Social.IsiAdCountry) {

            try {
                _ad = new MonoTouch.iAd.ADBannerView (MonoTouch.iAd.ADAdType.Banner);
                _ad.Hidden = true;
                _ad.FailedToReceiveAd += HandleFailedToReceiveAd;
                _ad.AdLoaded += HandleAdLoaded;
                View.BackgroundColor = UIColor.Clear;
                _anyVC.View.Frame = View.Bounds;
                View.AddSubview (_ad);
            } catch {
            }
        } else {
            Resize ();
        }
    }

    public override void DidRotate (UIInterfaceOrientation fromInterfaceOrientation)
    {
        base.DidRotate (fromInterfaceOrientation);
        Resize ();
    }

    public override void ViewDidAppear (bool animated)
    {
        base.ViewDidAppear (animated);
        Resize ();
    }

    void Resize ()
    {

        UIView.Animate (.25,
            () => {
                if (_ad !=null && _ad.Hidden == false) {
                    _anyVC.View.Frame = new RectangleF (0, 0, this.View.Bounds.Width, this.View.Bounds.Height - _ad.Frame.Height);
                } else {
                    _anyVC.View.Frame = View.Bounds;
                }
            });
        if(_ad!=null)
           _ad.Frame = new RectangleF (0, _anyVC.View.Bounds.Height, this.View.Bounds.Width, _ad.Frame.Height);
    }

    void HandleAdLoaded (object sender, EventArgs e)
    {
        if (_ad == null)
            return;
        _ad.Hidden = false;
        Resize ();
    }

    void HandleFailedToReceiveAd (object sender, AdErrorEventArgs e)
    {
        if (_ad == null)
            return;
        _ad.Hidden = true;
        Resize ();
    }
}

这篇关于Monotouch.Dialog和iAds的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 07:02