ios – 如何从GameScene中的ViewController调用方法

前端之家收集整理的这篇文章主要介绍了ios – 如何从GameScene中的ViewController调用方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个方法,在我的viewController中有一个自定义segue,如下所示:
  1. func gameOver() {
  2. performSegueWithIdentifier("GameOver",sender: nil)
  3. }

我在GameScene.swift中调用了这样的方法

  1. GameViewController().gameOver()

我仔细检查了segue名称,这是正确的.每当我在GameScene.swift文件调用它时,我都会收到SIGABRT消息,但我不知道为什么.我尝试只用println()消息调用函数,它工作.

关于为什么会发生这种情况以及如何在GameScene.swift文件中成功调用方法的任何建议将不胜感激.

谢谢!

附:这是崩溃日志:

  1. 2015-01-28 21:59:46.181 RunawaySquare[95616:3907041] *** Terminating app due to uncaught exception 'NSInvalidArgumentException',reason: 'Receiver (<RunawaySquare.GameViewController: 0x7fe4305c7890>) has no segue with identifier 'GameEnd''
  2. *** First throw call stack:
  3. (
  4. 0 CoreFoundation 0x000000010d461f35 __exceptionPreprocess + 165
  5. 1 libobjc.A.dylib 0x000000010f39ebb7 objc_exception_throw + 45
  6. 2 UIKit 0x000000010e20dd3b -[UIViewController shouldPerformSegueWithIdentifier:sender:] + 0
  7. 3 RunawaySquare 0x000000010d2683b2 _TFC13RunawaySquare18GameViewController8gameOverfS0_FT_T_ + 914
  8. 4 RunawaySquare 0x000000010d261af0 _TFC13RunawaySquare9GameScene12touchesBeganfS0_FTCSo5NSSet9withEventCSo7UIEvent_T_ + 1808
  9. 5 RunawaySquare 0x000000010d261c3f _TToFC13RunawaySquare9GameScene12touchesBeganfS0_FTCSo5NSSet9withEventCSo7UIEvent_T_ + 79
  10. 6 SpriteKit 0x000000010df4d7e1 -[SKView touchesBegan:withEvent:] + 946
  11. 7 UIKit 0x000000010e12d16e -[UIWindow _sendTouchesForEvent:] + 325
  12. 8 UIKit 0x000000010e12dc33 -[UIWindow sendEvent:] + 683
  13. 9 UIKit 0x000000010e0fa9b1 -[UIApplication sendEvent:] + 246
  14. 10 UIKit 0x000000010e107a7d _UIApplicationHandleEventFromQueueEvent + 17370
  15. 11 UIKit 0x000000010e0e3103 _UIApplicationHandleEventQueue + 1961
  16. 12 CoreFoundation 0x000000010d397551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
  17. 13 CoreFoundation 0x000000010d38d41d __CFRunLoopDoSources0 + 269
  18. 14 CoreFoundation 0x000000010d38ca54 __CFRunLoopRun + 868
  19. 15 CoreFoundation 0x000000010d38c486 CFRunLoopRunSpecific + 470
  20. 16 GraphicsServices 0x000000011480e9f0 GSEventRunModal + 161
  21. 17 UIKit 0x000000010e0e6420 UIApplicationMain + 1282
  22. 18 RunawaySquare 0x000000010d26cbee top_level_code + 78
  23. 19 RunawaySquare 0x000000010d26cc2a main + 42
  24. 20 libdyld.dylib 0x000000010fb8a145 start + 1
  25. )
  26. libc++abi.dylib: terminating with uncaught exception of type NSException

它说没有segue id.使用“GameEnd”,但有一个,如果在viewcontroller上使用它可以工作

解决方法

这不起作用的原因是你正在创建一个GameViewController的新实例,然后你就是在那上面调用gameOver.你真正想要做的是引用你现有的GameViewController

有几种方法可以做到这一点,我举一个例子.

将viewController属性添加到GameScene类

  1. class GameScene {
  2.  
  3. // we need to make sure to set this when we create our GameScene
  4. var viewController: GameViewController!

在你的GameViewController文件

  1. // after GameScene is instantiated
  2. gameScene.viewController = self

现在我们引用了viewController,让我们在GameScene类中使用它

  1. // somewhere in GameScene
  2. self.viewController.gameOver()

猜你在找的iOS相关文章