Reputation: 589
I'm currently working on a section of my game where the user is taken to a second ViewController
once they lose, this is my GameOverViewController
.
I've successfully set up the second view controller with an interstitial advert that runs almost instantly once the GameOverViewController
has loaded, the replay
button is then only active once the interstitial advert has been closed.
My app crashes once the replay button has been pressed, it worked fine before I added the delay so I'm guessing it's something to do with my new code. The replay button is performing a unwind segue (or trying to), would anyone be able to help resolve?
class GameOverViewController: UIViewController {
@IBOutlet weak var button: UIButton!
}
override func viewDidLoad() {
super.viewDidLoad()
self.button.enabled = false
NSTimer.scheduledTimerWithTimeInterval(3, target: self, selector: "enableButton", userInfo: nil, repeats: false)
//lots of code here to bring up google interstitial advert
}
func enableButton() {
self.button.enabled = true
}
The button is correctly greyed out for three seconds then turns blue, however once clicked the viewController hangs then crashes. The error brings up AppDelegate with the SIGABRT error. This is the extract from the output field...
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Ginger_Cat.GameOverViewController button:]: unrecognized selector sent to instance 0x7fe70739d120'
*** First throw call stack:
(
0 CoreFoundation 0x0000000111b4dc65 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000113b96bb7 objc_exception_throw + 45
2 CoreFoundation 0x0000000111b550ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x0000000111aab13c ___forwarding___ + 988
4 CoreFoundation 0x0000000111aaacd8 _CF_forwarding_prep_0 + 120
5 UIKit 0x00000001128cbd62 -[UIApplication sendAction:to:from:forEvent:] + 75
6 UIKit 0x00000001129dd50a -[UIControl _sendActionsForEvents:withEvent:] + 467
7 UIKit 0x00000001129dc8d9 -[UIControl touchesEnded:withEvent:] + 522
8 UIKit 0x0000000112918958 -[UIWindow _sendTouchesForEvent:] + 735
9 UIKit 0x0000000112919282 -[UIWindow sendEvent:] + 682
10 UIKit 0x00000001128df541 -[UIApplication sendEvent:] + 246
11 UIKit 0x00000001128eccdc _UIApplicationHandleEventFromQueueEvent + 18265
12 UIKit 0x00000001128c759c _UIApplicationHandleEventQueue + 2066
13 CoreFoundation 0x0000000111a81431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
14 CoreFoundation 0x0000000111a772fd __CFRunLoopDoSources0 + 269
15 CoreFoundation 0x0000000111a76934 __CFRunLoopRun + 868
16 CoreFoundation 0x0000000111a76366 CFRunLoopRunSpecific + 470
17 GraphicsServices 0x00000001151d0a3e GSEventRunModal + 161
18 UIKit 0x00000001128ca8c0 UIApplicationMain + 1282
19 Ginger Cat 0x000000010f9caa37 main + 135
20 libdyld.dylib 0x00000001142f0145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb)
Here is the code in my GameViewController, there is currently no other code relating to the unwind in this ViewController
@IBAction func replay(segue: UIStoryboardSegue) {
}
Any help would be great, thanks.
Upvotes: 1
Views: 1723
Reputation: 37053
The stack trace from the exception is telling you that you are calling the method button(_:)
on your GameOverViewController
, and that method does not exist (known as an "unrecognized selector" from Objective-C parlance).
It is not immediately clear where this is happening in your code, but I'm guessing that since you say the crash happens when you tap the button, there is an unintentional action called button(_:)
connected to a touch event on your button in your storyboard. Select your button in the storyboard and choose the connections inspector on the right. Look for an action called button:
- that could be the cause of the problem.
As a guess as to how this happened - did your replay(_:)
unwind segue used to be called button(_:)
, and then you renamed it? Renamed methods in code aren't updated automatically in the storyboard, and can be a common source of bugs due to bad connections between the storyboard and code.
Upvotes: 3
Reputation: 1661
I think there is issue with you IBAction. Code below might be helpful.
@IBAction func replay(sender: UIButton) {
self.performSegueWithIdentifier("yourunwindidentifername", sender: self)
}
Upvotes: 0