尝试给我们 UIAlertController 打分
trying to do a rate us UIAlertController
每当我 运行 我的应用程序调用以下错误时
警告:试图在视图不在 window 层次结构中的 "MenuViewController" 上呈现 "The alert"!
这到底是什么意思,我该如何解决?
我只有一个 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
}
}
在 viewDidLoad 中,您的视图尚未显示。将给我评分的呼叫从 viewDidLoad 移动到 viewDidAppear。
编辑
因为它是我的评分。我认为最好将它放在 applicationDidFinishLaunchWithOptions 的 appDelegate 中。
每当我 运行 我的应用程序调用以下错误时
警告:试图在视图不在 window 层次结构中的 "MenuViewController" 上呈现 "The alert"!
这到底是什么意思,我该如何解决?
我只有一个 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
}
}
在 viewDidLoad 中,您的视图尚未显示。将给我评分的呼叫从 viewDidLoad 移动到 viewDidAppear。
编辑
因为它是我的评分。我认为最好将它放在 applicationDidFinishLaunchWithOptions 的 appDelegate 中。