Quantcast
Channel: How to create an alert box in iphone? - Stack Overflow
Viewing all articles
Browse latest Browse all 11

Answer by jboi for How to create an alert box in iphone?

$
0
0

The post is quite old, but still a good question. With iOS 8 the answer has changed. Today you'd rather use 'UIAlertController' with a 'preferredStyle' of 'UIAlertControllerStyle.ActionSheet'.

A code like this (swift) that is bound to a button:

@IBAction func resetClicked(sender: AnyObject) {
    let alert = UIAlertController(
        title: "Reset GameCenter Achievements",
        message: "Highscores and the Leaderboard are not affected.\nCannot be undone",
        preferredStyle: UIAlertControllerStyle.ActionSheet)
    alert.addAction(
        UIAlertAction(
            title: "Reset Achievements",
            style: UIAlertActionStyle.Destructive,
            handler: {
                (action: UIAlertAction!) -> Void in
                gameCenter.resetAchievements()
            }
        )
    )
    alert.addAction(
        UIAlertAction(
            title: "Show GameCenter",
            style: UIAlertActionStyle.Default,
            handler: {
                (action: UIAlertAction!) -> Void in
                self.gameCenterButtonClicked()
            }
        )
    )
    alert.addAction(
        UIAlertAction(
            title: "Cancel",
            style: UIAlertActionStyle.Cancel,
            handler: nil
        )
    )
    if let popoverController = alert.popoverPresentationController {
        popoverController.sourceView = sender as UIView
        popoverController.sourceRect = sender.bounds
    }
    self.presentViewController(alert, animated: true, completion: nil)
}

would produce this output: enter image description here

EDIT: The code crashed on iPad, iOS 8+. If added the necessary lines as described here: on another stack overflow answer


Viewing all articles
Browse latest Browse all 11

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>