Displaying a UIAlertController in GameScene (SpriteKit/Swift)

后端 未结 3 911
青春惊慌失措
青春惊慌失措 2021-01-15 00:39

The simple way to display a UIAlertView, in swift is:

let alert = UIAlertView()
alert.title = \"Alert!\"
alert.message = \"A wise message\"
alert.addButtonWi         


        
3条回答
  •  不思量自难忘°
    2021-01-15 01:32

    The answer from @Knight0fDragon is nice, but I think it's a little long. I will just put here another solution using Podfile of Cocoapoad for new comers (others guys having the same problem).

    1. first you need to install cocoapod and initiate it for your project (very easy to do; check some YouTube video or this link.

    2. in your obtained Podfile, copy, and paste this: pod 'SIAlertView'. It is "A UIAlertView replacement with block syntax and fancy transition styles". (More details here. Please give credit to the libraries Authors you're using.

    3. Finally, in your GameScene.swift file add the following after the import or after the closing bracket of the class GameScene

      private extension GameScene {
          func showPauseAlert() {
              let alertView = SIAlertView(title: "Edess!!", andMessage: "Congratulations! test testing bla bla bla")
      
              alertView.addButtonWithTitle("OK", type: .Default) { (alertView) -> Void in
                  print("ok was pushed")
              }
      
              alertView.show()
          }
      }
      

    You can add many button with title if you want, and do whatever action you want. Here I just print "Ok was pushed".

    The above cited links plus this one helped me to understand and work with this UIAlertView easily in my multiple-level game, to display alert on the "Pause" button of the game.

提交回复
热议问题