在你GameViewController
,設置觀察者在viewWillLayoutSubviews
:
override func viewWillLayoutSubviews() {
NotificationCenter.default.addObserver(self, selector: #selector(self.startVideoAd), name: NSNotification.Name(rawValue: "showVideoRewardAd"), object: nil)
}
func startVideoAd() {
// Do something - play video ad
}
在這種情況下,每當這個通知被調用時, GameViewController
裏面的函數名爲startVideoAd
將會運行。很明顯,你會想把名字改成你想要運行的函數的名字。
NotificationCenter.default.post(name: NSNotification.Name(rawValue: "showVideoRewardAd"), object: nil)
希望這有助於:
然後,在你GameScene
,發送通知,你會無論/只要你想運行裏面GameViewController
功能運行此!
來源
2016-11-03 18:57:14
Nik
你必須從GameViewController中展示它。使用通知讓SKScene告訴GameViewController啓動視頻廣告 – Nik
@Nik我試圖在GameViewController中創建函數,並且不會調用GameScene中的函數,但這不起作用。當它不得不調用函數時,它給了我同樣的錯誤。 –
使用通知在場景和視圖控制器之間進行通信。我會盡快答覆 – Nik