2
我正在使用NSNotificationCenter試圖控制SpriteKit中的計時器。當我第一次進入SKScene時代碼運行良好,但是當我嘗試並重新進入SKScene時,我得到一個EXC_BAD_ACCESS錯誤。我認爲這與removeObserver函數有關。我不確定何時刪除觀察者,我試圖在prepareForSegue函數中做到這一點,但沒有成功。我的viewController如下:NSNotificationCenter導致與SpriteKit EXC_BAD_ACCESS錯誤
class JobStartedViewController: UIViewController {
var titleOfJob: String = ""
override func viewDidLoad() {
super.viewDidLoad()
let skView = self.view as! SKView
let scene:SKScene = GameScene.init(size: skView.bounds.size)
NSNotificationCenter.defaultCenter().postNotificationName("stopTimerNotification", object: nil)
NSNotificationCenter.defaultCenter().postNotificationName("startTimerNotification", object: nil)
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .AspectFill
skView.presentScene(scene)
}
,並添加我觀察到我的GameScene.swift如下:
class GameScene: SKScene {
override func didMoveToView(view: SKView) {
NSNotificationCenter.defaultCenter().addObserver(self, selector: "stopTimerInBackground:", name:"stopTimerNotification", object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "startTimerInBackground:", name:"startTimerNotification", object: nil)
您需要刪除'deinit'中的觀察者。 –