Swift-从GADBannerView扩展

时间:2019-05-08 09:31:12

标签: swift oop inheritance admob extend

我想构建像其他视图一样扩展GADBannerView的自定义类,但是它不起作用

class ListAd:GADBannerView {
    var controller: UIViewController?

    convenience init(hostView: UIView, controller: UIViewController) {
        self.init(frame: hostView.bounds)
        self.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.translatesAutoresizingMaskIntoConstraints = false
        self.controller = controller
        hostView.addSubview(self)
    }

    func loadAd() {
        if APP.displayBannerAd {
            self.adUnitID = ADMOBIDS["banner"]
            self.rootViewController = controller
            self.load(GADRequest())
        }
    }
}

然后我将其初始化

admobBanner = ListAd(hostView: self.view, controller: self)
admobBanner?.loadAd()

得到错误:

[App.ListAd setAdUnitID:]: unrecognized selector sent to instance 0x7fe7f9c04d60'

1 个答案:

答案 0 :(得分:0)

尝试致电init(adSize: GADAdSize, origin: CGPoint)init(adSize: GADAdSize)而不是self.init(frame:)