问题
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