-1

This is probably a very simple question, but here it is: how do I make it so that when a user taps "Discard" (in the navigation bar) a little box pops up that makes him confirm that he wants to discard what he's working on? I would like it to look like that notification box that tells you you're out of storage in the built in camera app, but instead, have it say something like "Are you sure you want to discard this layout?" and then have one button be "yes" and the other be "no". What is the code for doing this? I am using Swift.

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
TonyStark4ever
  • 848
  • 1
  • 9
  • 24

1 Answers1

2

You are looking for UIAlertController. Here's a example:

let alertController = UIAlertController(title: "Title Text",
                                           message: "Message Text",
                                    preferredStyle: UIAlertControllerStyle.alert)

let okButton = UIAlertAction(title: "OK",
                                    style: .default,
                                  handler: { (action:UIAlertAction!) in
     //do your action here
})
alertController.addAction(okButton)

let cancelButton = UIAlertAction(title: "Cancel", style: .cancel, handler: { (action:UIAlertAction!) in
    })
alertController.addAction(cancelButton)

self.present(alertController, animated: true, completion:nil)
Arpit Dongre
  • 1,683
  • 19
  • 30
  • Thanks. How do I add an unwind segue for the "ok" action? Also, what is the code? Specifically, I have a view between the current one and the one I want to unwind to. I've been playing with it for an hour and I can't figure it out. Thanks. – TonyStark4ever Jun 04 '17 at 04:52
  • @W.Cook, see [this](https://stackoverflow.com/questions/24022479/how-would-i-create-a-uialertview-in-swift/33340757#33340757) and [this](https://stackoverflow.com/questions/4988564/how-to-implement-a-pop-up-dialog-box-in-ios). You don't have an unwind segue with an alert controller. You have a handler action function that is run with the user clicks on of the buttons. – Suragch Jun 04 '17 at 14:16
  • @Suragch So can I make my handler action function take the user back to a previous screen? If so, how? Thanks. – TonyStark4ever Jun 04 '17 at 19:33
  • @Suragch I got it! I called performSegueWithIdentifier, and that works. Thanks. – TonyStark4ever Jun 05 '17 at 02:14
  • @W.Cook, Ah, I didn't understand what you meant. I'm glad you figured it out. – Suragch Jun 05 '17 at 08:30