尝试在视图不在窗口层次结构中的<uiviewcontroller>上显示<uialertcontroller>

时间:2018-08-16 19:42:44

标签: swift uiviewcontroller uialertcontroller

当节点掉落在错误的目标上时,我试图在游戏中触发警报。

这是GameScene.swift中的样子:

else if label.name == "letters"{
    if numbersBin.frame.contains(label.position){
        showAlert(withTitle: "Try again!", message: "Remember, letters are A, B, C...")
        }

这是GameViewController.swift中的样子:

import UIKit
import SpriteKit
import GameplayKit

class GameViewController: UIViewController {

override func viewDidLoad() {

    super.viewDidLoad()

    if let view = self.view as! SKView? {
        // Load the SKScene from 'GameScene.sks'
        if let scene = SKScene(fileNamed: "GameScene") {
            // Set the scale mode to scale to fit the window
            scene.scaleMode = .aspectFill

            // Present the scene
            view.presentScene(scene)
        }

        view.ignoresSiblingOrder = true

        view.showsFPS = true
        view.showsNodeCount = true
    }
}

func viewDidAppear(){
    let game = GameScene()
    game.viewController = self
}

override var shouldAutorotate: Bool {
    return true
}

override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
    if UIDevice.current.userInterfaceIdiom == .phone {
        return .allButUpsideDown
    } else {
        return .all
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

override var prefersStatusBarHidden: Bool {
    return true
}
}

这是我从crashoverride77's answer获取的Alerts.swift文件:     导入SpriteKit

protocol Alertable { }
extension Alertable where Self: SKScene {

func showAlert(withTitle title: String, message: String) {

    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)

    let okAction = UIAlertAction(title: "OK", style: .cancel) { _ in }
    alertController.addAction(okAction)

    view?.window?.rootViewController?.present(alertController, animated: true)
}

func showAlertWithSettings(withTitle title: String, message: String) {

    let alertController = UIAlertController(title: title, message: message, preferredStyle: .alert)

    let okAction = UIAlertAction(title: "OK", style: .cancel) { _ in }
    alertController.addAction(okAction)

    let settingsAction = UIAlertAction(title: "Settings", style: .default) { _ in

        guard let url = URL(string: UIApplicationOpenSettingsURLString) else { return }
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url)
        } else {
            UIApplication.shared.openURL(url)
        }
    }
    alertController.addAction(settingsAction)

    view?.window?.rootViewController?.present(alertController, animated: true)
}
}

不幸的是,标题中仍然出现“警告:尝试演示...”错误。我以为问题是一个viewDidLoad()/ viewDidAppear()一个,但是并没有解决任何问题。还有其他解决方案吗?

0 个答案:

没有答案
相关问题