Present Alert - Swift SpriteKit

Hi!

I want to make an “are you sure”-alert that pops up when the user touches a “reset highscore”-button.
I’ve searched the internet and have written the following code:

let alert = UIAlertController(title: "Reseting", message: "Are you sure?", preferredStyle: UIAlertControllerStyle.ActionSheet)
alert.addAction(UIAlertAction(title: "Yes", style: .Destructive, handler: alertButtonHandler))

I still don’t know how to make the alert appear.
Also, the handler is just a block of code written as a function. I haven’t really figured out how it works.

The given code is written inside a touchEnded() function.
I don’t have a storyboard. Thus, I’m creating the button programatically.

In advance, thanks!

You could try something like this

self.view?.window?.rootViewController?.presentViewController(alert, animated: true, completion: nil)

The two general ways of doing this in SpriteKit are to either create methods in the ViewController class (GameViewController for example) > register for notifications on those methods > “Post” to NSNotification from the SKScene when you want to display something from UIKIt.

The other is to simply call up the chain to the presenting viewcontroller (as given above).

However, the basic gist is that the main UIViewController has to call it. It helps to understand that SKScenes are running inside this view controller.

It worked!
I used your line of code :smile:
Thank you!