我目前在我的GameViewController中用下面的代码从admob中调用一个Banner广告。
class GameViewController: UIViewController {
var bannerView: GADBannerView!
}
override func viewDidLoad() {
super.viewDidLoad()
bannerView = GADBannerView(adSize: kGADAdSizeBanner)
addBannerViewToView(bannerView)
bannerView.adUnitID = "ca-app-pub-XXXXXXXXX" // Default Google Test Ad Address
bannerView.rootViewController = self
bannerView.load(GADRequest())
bannerView.isHidden = false // <----- This works, but doesn't work in GameScene.swift
这是从上面的代码(也是在GameViewController中)调用的函数来显示广告。
func addBannerViewToView(_ bannerView: GADBannerView) {
bannerView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(bannerView)
view.addConstraints(
[NSLayoutConstraint(item: bannerView,
attribute: .bottom,
relatedBy: .equal,
toItem: bottomLayoutGuide,
attribute: .top,
multiplier: 1,
constant: 0),
NSLayoutConstraint(item: bannerView,
attribute: .centerX,
relatedBy: .equal,
toItem: view,
attribute: .centerX,
multiplier: 1,
constant: 0)
])
}
这段代码可以工作并显示测试广告。然而,在GameScene.swift中,当我按下一个按钮时,我运行这段代码来尝试隐藏广告。
GameViewController().bannerView.isHidden = true
这将导致应用程序崩溃,"Fatal error: Unexpected found nil while implicitly unwrapping an Optional value"。而如果我改变它,它不会崩溃,但广告不会隐藏。
GameViewController().bannerView?.isHidden = true
然而在GameViewController中,如果我简单地将isHidden的值改为true,那么它就会工作。
bannerView.isHidden = true
无论如何,当我按下按钮时,能够在GameScene.swift中隐藏横幅广告?
当你在调用 GameViewController()
您正在创建一个新的 GameViewController
.
关于 GameViewController().bannerView
您正在访问的 bannerView
由于它是一个可选的值,没有在这个实例上设置,当你试图访问它时,会导致崩溃。
你需要的是访问主 GameViewController
在这里,你有 bannerView
集。
有两个简单的方法可以调用 GameViewController
其他类的函数:通过 delegate
或通过使类实例 static
.
// DELEGATE
//
// You have access only to the functions you declared inside the procotol.
// Only the classes that conforms to that protocol have access to the functions.
//
// GameViewController.swift
protocol GameView_Delegate
{
func hide_ads()
}
class GameViewController: UIViewController, GameView_Delegate
{
override func viewDidLoad()
{
// ...
if let view = self.view as! SKView?
{
if let scene = SKScene(fileNamed: "GameScene")
{
scene.scaleMode = .aspectFill
// pass protocol reference
scene.gv_delegate = self
}
view.presentScene(scene)
}
// ...
}
func hide_ads()
{
// hide ads code
bannerView.isHidden = true
}
}
// GameScene.swift
class GameScene: SKScene
{
var gv_delegate : GameView_Delegate!
func settings()
{
// call function hide_ads from GameViewController
gv_delegate.hide_ads()
}
func lost_scene()
{
let transition = SKTransition.fade(with: UIColor.white, duration: 1.0)
let next_scene = LostScene()
next_scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
next_scene.scaleMode = self.scaleMode
next_scene.size = self.size
// before moving to next scene, we must pass the delegate, so we
// don't lose the connection with the GameViewController
next_scene.gv_delegate = gv_delegate
self.view?.presentScene(next_scene, transition: transition)
}
}
// LostScene.swift
class LostScene: SKScene
{
var gv_delegate : GameView_Delegate!
func settings()
{
// call function hide_ads from GameViewController
gv_delegate.hide_ads()
}
func game_scene()
{
let transition = SKTransition.fade(with: UIColor.white, duration: 1.0)
let next_scene = GameScene()
next_scene.anchorPoint = CGPoint(x: 0.5, y: 0.5)
next_scene.scaleMode = self.scaleMode
next_scene.size = self.size
// before moving to next scene, we must pass the delegate, so we
// don't lose the connection with the GameViewController
next_scene.gv_delegate = gv_delegate
self.view?.presentScene(next_scene, transition: transition)
}
}
当你进入一个新的场景时,例如 LostScene
昔日的场景 GameScene
在我们的例子中,是deallocated(清空所有变量,销毁所有引用),并建立一个新的 GameScene
实例被创建并显示在屏幕上。在过渡到新的场景之前,你应该传递引用,这样你就不会失去与新场景的连接。GameViewController
(如我的例子)。
// STATIC
//
// the properties and functions are available from everywhere
//
// GameViewController.swift
class GameViewController: UIViewController
{
static var shared : GameViewController!
override func viewDidLoad()
{
super.viewDidLoad()
GameViewController.shared = self
// ...
}
func hide_ads()
{
// hide ads code
bannerView.isHidden = true
}
}
// GameScene.swift
class GameScene: SKScene
{
func settings()
{
// call function hide_ads from GameViewController
GameViewController.shared.hide_ads()
// or GameViewController.shared.bannerView.isHidden = true
}
}
你正在创建一个新的 GameViewController
和切换它的 bannerView
's isHidden
属性。你应该把 isHidden
的财产 bannerView
的 GameViewController
的实例。
如果有人想知道,这是我找到的隐藏横幅的解决方案。
"在GameViewController.swift文件中处理所有的横幅内容" "在那里,为bannerView变量分配一个标签值 在那里里面,为bannerView变量分配一个标签值。然后当场景加载时,使用标签id加载bannerView,然后将其.isHidden属性设置为true"
GameViewController.swift
bannerView.tag = 100
GameScene.swift
// hide banner
let bannerView = self.view?.viewWithTag(100) as! GADBannerView?
bannerView?.isHidden = true
// show banner
let bannerView = self.view?.viewWithTag(100) as! GADBannerView?
bannerView?.isHidden = false