简体   繁体   English

提供一个带有SpriteKit场景的UIAlertController(Objective-C)

[英]Present an UIAlertController with a SpriteKit scene (Objective-C)

I'm trying to make a SpriteKit app that has a button to go to my website if the user so desires to. 我正在尝试制作一个SpriteKit应用程序,该应用程序具有一个按钮,如果用户愿意的话可以访问我的网站。 I thought it would be a good practice to make an alert (since it's made for iOS 8, I'd use a UIAlertController ) confirming if they want to be redirected to Safari to see it. 我认为发出警报(因为它是针对iOS 8的,因此我将使用UIAlertController )是一个很好的做法,以确认是否希望将其重定向到Safari来查看它。

Here's what I have in code for the method to make/present this alert so far: 到目前为止,这是我在代码中用于发出/发出此警报的方法的内容:

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Go to website" message:@"Would you like to visit DannyDalton.com? This will open Safari." preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *yes = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
            [self goToSite];
        }];
        UIAlertAction *no = [UIAlertAction actionWithTitle:@"No" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){}];
        [alertController addAction:yes];
        [alertController addAction:no];
        [alertController presentViewController:(UIViewController*)self animated:YES completion:nil];

My problem is presenting alertController onto the view controller, so it pops up. 我的问题是将alertController呈现到视图控制器上,因此它弹出了。 Any suggestions on how to make the SKView into a UIViewController so that alertController can present itself onto the screen? 关于如何使SKView成为UIViewController以便alertController可以将自己呈现在屏幕上的任何建议? Thank you! 谢谢!

One way to do this is to use NSNotification . 一种方法是使用NSNotification

In the GameViewController (containing SKView ) add a NSNotification observer to call the function to show the alert when receiving the notification. GameViewController (含有SKView )添加一个NSNotification观察者调用的函数接收到该通知时,显示警报。

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "showAlert:", name: "showAlert", object: nil)
     }

    func showAlert(object : AnyObject) {

       // Your alert code.
    }
}

Then post this notification from the place where you want to show the alert. 然后从您要显示警报的地方发布此通知。

NSNotificationCenter.defaultCenter().postNotificationName("showAlert", object: nil)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM