试图给我们评分UIAlertController

时间:2015-06-17 22:16:41

标签: ios iphone swift uialertcontroller

每当我运行我的应用程序时,都会调用以下错误

警告:尝试在“MenuViewController”上显示“警报”,其视图不在窗口层次结构中!

这究竟是什么意思,我该如何解决?

我只有一个UIViewController,这里是

class MenuViewController: UIViewController {


    var iMinSessions = 3
    var iTryAgainSessions = 6



    func rateMe() {
        var neverRate = NSUserDefaults.standardUserDefaults().boolForKey("neverRate")
        var numLaunches = NSUserDefaults.standardUserDefaults().integerForKey("numLaunches") + 1

        if (!neverRate && (numLaunches == iMinSessions || numLaunches >= (iMinSessions + iTryAgainSessions + 1)))
        {
            showRateMe()
            numLaunches = iMinSessions + 1
        }
        NSUserDefaults.standardUserDefaults().setInteger(numLaunches, forKey: "numLaunches")
    }





    func showRateMe() {
        var alert = UIAlertController(title: "Rate Me", message: "Thanks for playing Pong Ball", preferredStyle: UIAlertControllerStyle.Alert)

        alert.addAction(UIAlertAction(title: "Rate Pong Ball!", style: UIAlertActionStyle.Default, handler: { alertAction in
            UIApplication.sharedApplication().openURL(NSURL(string : "itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=<iTUNES CONNECT APP ID>")!)

            alert.dismissViewControllerAnimated(true, completion: nil)
        }))

        alert.addAction(UIAlertAction(title: "No Thanks", style: UIAlertActionStyle.Default, handler: { alertAction in
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "neverRate")

            alert.dismissViewControllerAnimated(true, completion: nil)

        }))
        alert.addAction(UIAlertAction(title: "Maybe Later", style: UIAlertActionStyle.Default, handler: { alertAction in
            alert.dismissViewControllerAnimated(true, completion: nil)
        }))


        self.presentViewController(alert, animated: true, completion: nil)
    }






    override func viewDidLoad() {
        super.viewDidLoad()

//        Calling it

        rateMe()

        let skView = view as! SKView
        var scene = MenuScene(size: skView.bounds.size)
        skView.ignoresSiblingOrder = true
        scene.scaleMode = .AspectFill
        skView.presentScene(scene)
            }

    override func supportedInterfaceOrientations() -> Int {
        if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
            return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
        } else {
            return Int(UIInterfaceOrientationMask.All.rawValue)
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
}

1 个答案:

答案 0 :(得分:2)

在viewDidLoad中,您的视图尚未显示。移动调用以将其从viewDidLoad评级为viewDidAppear。

修改

因为这是我的评价。我认为它更好地放在applicationDidFinishLaunchWithOptions中的appDelegate中。