0
我想在drawRect UIView中放置一個橫幅,但它在'bannerView.rootViewController = self'處發出警告。所以我把橫幅放在它的viewController中。我希望drawRect中的值更改橫幅的位置。該值可以打印出來,但橫幅拒絕移動。這裏是代碼:UIView中的協議不能更改viewController中的對象?
的ViewController:
class ViewController: UIViewController, GADBannerViewDelegate,ChangeBannerDelegate
// var bannerView:GADBannerView! // shows nil when run func changeBanner(), has to use the following
var bannerView = GADBannerView(adSize: kGADAdSizeBanner)
override func viewDidLoad() {
super.viewDidLoad()
waveView = WaveWater(frame: self.view.bounds)
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
bannerView.frame = CGRect(x: 0, y: self.view.bounds.height-50, width: self.view.bounds.width, height: 50)
bannerView.adUnitID = "xxxxxxxxxxxx"
bannerView.rootViewController = self
bannerView.delegate = self
self.view.addSubview(bannerView)
bannerView.loadRequest(GADRequest())
// set other things...
}
func changeBanner(y:CGFloat){
bannerView.frame.origin.y = y
println(bannerView.frame.origin.y) // can print 'y', and banner.frame.origin.y was changed, but banner stays where it is
}
的UIView:
protocol ChangeBannerDelegate:NSObjectProtocol,GADBannerViewDelegate{
func changeBanner(y:CGFloat)
}
class WaveWater: UIView, UIGestureRecognizerDelegate {
var bannerDelegate:ChangeBannerDelegate?
override init(frame: CGRect) {
super.init(frame: frame)
self.bannerDelegate = ViewController()
changeBanner(50) // place it here just for a try
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func drawRect(rect: CGRect) {
//drawing things........
}
}
它的偉大工程!顯示我想要的。謝謝。 – jdleung