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

淺唱寂寞╮ 提交于 2019-12-07 20:51:56

问题


I'm trying to make a SpriteKit app that has a button to go to my website if the user so desires to. 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.

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. Any suggestions on how to make the SKView into a UIViewController so that alertController can present itself onto the screen? Thank you!


回答1:


One way to do this is to use NSNotification.

In the GameViewController(containing SKView) add a NSNotification observer to call the function to show the alert when receiving the notification.

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)


来源:https://stackoverflow.com/questions/29159008/present-an-uialertcontroller-with-a-spritekit-scene-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!