Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
376 views
in Technique[技术] by (71.8m points)

ios - How to call method from ViewController in GameScene

I have a method that has a custom segue in my viewController that looks like this:

func gameOver() {
    performSegueWithIdentifier("GameOver", sender: nil)
}

I call the method like so in GameScene.swift:

 GameViewController().gameOver()

I double checked the segue name and it is correct. Whenever I call this in my GameScene.swift file I get the SIGABRT message and I don't know why. I tried calling the function with only a println() message and it worked.

Any advice on why this is occurring and how I can successfully call the method in the GameScene.swift file would be greatly appreciated.

Thanks!

P.S. here is the crash log:

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

it says there is no segue id. with "GameEnd" but there is one and it works if used on the viewcontroller

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

the reason this doesnt work is that you are creating a NEW instance of GameViewController and then you're calling gameOver on that. What you really want to do is reference your existing GameViewController

theres a few ways to do this, I'll give you one example.

add a viewController property to your GameScene class

class GameScene {

    // we need to make sure to set this when we create our GameScene
    var viewController: GameViewController!

in your GameViewController file

// after GameScene is instantiated
gameScene.viewController = self

now we have a reference to viewController, lets use it in our GameScene class

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...