我的viewController中有一个自定义segue的方法,如下所示:
func gameOver() {
performSegueWithIdentifier("GameOver", sender: nil)
}
我在GameScene.swift中调用了这样的方法:
GameViewController().gameOver()
我仔细检查了segue名称,这是正确的。每当我在GameScene.swift文件中调用它时,我都会收到SIGABRT消息,但我不知道为什么。我尝试只使用println()消息调用该函数,并且它有效。
对于为什么会发生这种情况以及如何在GameScene.swift文件中成功调用该方法的任何建议都将不胜感激。
谢谢!
P.S。这是崩溃日志:
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''
*** First throw call stack:
(
0 CoreFoundation 0x000000010d461f35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010f39ebb7 objc_exception_throw + 45
2 UIKit 0x000000010e20dd3b -[UIViewController shouldPerformSegueWithIdentifier:sender:] + 0
3 RunawaySquare 0x000000010d2683b2 _TFC13RunawaySquare18GameViewController8gameOverfS0_FT_T_ + 914
4 RunawaySquare 0x000000010d261af0 _TFC13RunawaySquare9GameScene12touchesBeganfS0_FTCSo5NSSet9withEventCSo7UIEvent_T_ + 1808
5 RunawaySquare 0x000000010d261c3f _TToFC13RunawaySquare9GameScene12touchesBeganfS0_FTCSo5NSSet9withEventCSo7UIEvent_T_ + 79
6 SpriteKit 0x000000010df4d7e1 -[SKView touchesBegan:withEvent:] + 946
7 UIKit 0x000000010e12d16e -[UIWindow _sendTouchesForEvent:] + 325
8 UIKit 0x000000010e12dc33 -[UIWindow sendEvent:] + 683
9 UIKit 0x000000010e0fa9b1 -[UIApplication sendEvent:] + 246
10 UIKit 0x000000010e107a7d _UIApplicationHandleEventFromQueueEvent + 17370
11 UIKit 0x000000010e0e3103 _UIApplicationHandleEventQueue + 1961
12 CoreFoundation 0x000000010d397551 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
13 CoreFoundation 0x000000010d38d41d __CFRunLoopDoSources0 + 269
14 CoreFoundation 0x000000010d38ca54 __CFRunLoopRun + 868
15 CoreFoundation 0x000000010d38c486 CFRunLoopRunSpecific + 470
16 GraphicsServices 0x000000011480e9f0 GSEventRunModal + 161
17 UIKit 0x000000010e0e6420 UIApplicationMain + 1282
18 RunawaySquare 0x000000010d26cbee top_level_code + 78
19 RunawaySquare 0x000000010d26cc2a main + 42
20 libdyld.dylib 0x000000010fb8a145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
它说没有segue id。用&#34; GameEnd&#34;但是有一个,如果在viewcontroller上使用它可以工作
答案 0 :(得分:26)
这不起作用的原因是您正在创建GameViewController
的新实例,然后您就此调用gameOver
。您真正想要做的是参考您现有的GameViewController
有几种方法可以做到这一点,我将举一个例子。
将viewController属性添加到GameScene类
class GameScene {
// we need to make sure to set this when we create our GameScene
var viewController: GameViewController!
GameViewController文件中的
// after GameScene is instantiated
gameScene.viewController = self
现在我们有一个对viewController的引用,让我们在GameScene类中使用它
// somewhere in GameScene
self.viewController.gameOver()
答案 1 :(得分:6)
我是通过创建协议完成的, 我有3个游戏场景(GameScene,GamePlayScene,GameEndScene)和一个游戏控制器(GameViewController)
首先创建gameProtocol
protocol GameDelegate {
func gameOver()
}
在GameViewController中实现协议
class GameViewController: UIViewController, GameDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let scene = GameScene(size: skView.bounds.size)
scene.scaleMode = .AspectFill
scene.delegate = self
}
// MARK: Game Delegate
func gameOver() {
self.performSegueWithIdentifier("yoursegue", sender: self)
}
}
将委托属性放在GameScene类
中class GameScene: SKScene {
var delegate: GameDelegate?
// call GamePlayScene and put delegate property
func redirectToPlay() {
let transition = SKTransition.pushWithDirection(SKTransitionDirection.Left, duration: 1.0)
let menuScene = GamePlayScene(size: size)
menuScene.delegate = self.delegate
self.view?.presentScene(menuScene, transition: transition)
}
}
并将协议放入GamePlayScene
class GamePlayScene: SKScene {
var delegate: GameDelegate?
// call GameEndScene and put delegate property
func gameScore() {
let transition = SKTransition.pushWithDirection(SKTransitionDirection.Left, duration: 1.0)
let menuScene = GameEndScene(size: size)
menuScene.delegate = self.delegate
self.view?.presentScene(menuScene, transition: transition)
}
}
最后,放置委托属性并调用gameOver函数
class GameEndScene: SKScene {
var delegate: GameDelegate?
init(size: CGSize) {
// call gameOver function
self.delegate?.gameOver()
}
}
希望工作能够帮助你
此致 唐尼
答案 2 :(得分:1)
我不知道这是否仍然相关,但我想向您介绍这个问题的解决方案。
正如您在此处看到的Swift iOS: Perform a Segue from an Instance in a ViewController to another ViewController我前段时间遇到了完全相同的问题,我使用协议设法解决了这个问题。
问题是,你可以调用&#34; performSegueWithIdentifier(&#34; GameOver&#34 ;, sender:nil)&#34;只在您的GameViewController类中,但您想从Gamescene中执行它。
因此,您可以在GameScene中创建一个这样的协议:
@objc protocol GameOverDelegate {
func gameOverDelegateFunc()
}
和Gamescene中代表的变量:
var gamescene_delegate : GameOverDelegate?
在你的GameViewController类中,你必须在类定义中添加委托
class GameViewController: UIViewController, GameOverDelegate {
...
}
并将GameViewController的viewDidLoad函数中场景的委托设置为self:
scene.gamescene_delegate = self
最后一步是在GameViewController中实现 gameOverDelegateFunc()函数:
func gameOverDelegateFunc() {
self.performSegueWithIdentifier("GameOver", sender: nil)
}
这就是你所要做的一切。
每当你想在你的GameScene中执行这个Segue时,你只需通过代表调用这个函数:
gamescene_delegate?.gameOverDelegateFunc()
我希望一切都清楚,我可以提供帮助,
此致 菲尔
答案 3 :(得分:0)
我没有足够的回复来回复您的上一条评论,但如果您仍然收到该错误,那是因为您没有给出一个名称(或者标识符)在Interface Builder中!
当您单击并拖动以在Interface Builder中的视图控制器之间绘制segue时,您可以通过出现在segue线中心的圆圈图标选择每个过渡,并为其指定一个标识符&#39;。然后,当你致电performSegueWithIdentifier
时,它应该有用!
这是圆形图标的样子,具体取决于segue的类型:
如果您对segues不太了解,请查看this tutorial或this one!