How do I configure UIAlert to display a lot of information?

I want to set up UIAlert as a "validation" for the user. The user is going to save a set of information, but I would like to enable the user to view the information after clicking the Save button. Basically, they click save, and then I want the UIAlert to pop up showing the information they are saving, asking "Are you sure all this information is correct:" and then display all the information:

@IBAction func saveButtonTapped(_ sender: UIBarButtonItem) {
        //create alert (I know how to do this) that shows all info (I don't know how to do this)
        //If user is ok with the info, perform a segue, if not return to page
}

      

The problem is that I know I UIAlerts

can have components like "title" and "message", but I would like the alert to display a lot of information in the list, like the mod. Is there any way to do this? Or do I need to not use the alert and instead perhaps lead it to a different confirmation page, or perhaps a different UI element?

+3


source to share


2 answers


The following code can be used to show a warning on the view controller.

let a = UIAlertController(title: "Your title", message: "Your message", preferredStyle: .alert)
a.addAction(UIAlertAction(title: "OK", style: .default, handler: { action in
    // Pressed "OK"
}))
self.present(a, animated: true, completion: { finished in
    // Alert shown
})

      

However, there is a limit to how much information can be placed inside an alert. If you have anything else, creating a new view controller and presenting it might work as well. This will allow you to customize how you want the information to be presented (e.g. with a scroll / page view). You can also customize your view controller to look like an alert so the purpose is clearer. To modularize a view controller, you can use the present

.

self.present(otherViewController, animated: true, completion: nil)

      

One of the techniques I used along the way to create a modal-like view manager is to have a smaller view controller above the current one and change its modal presentation style so you can see the base view controller through it.

let otherVC = self.storyboard?.instantiateViewController(withIdentifier: "OtherViewController") as! OtherViewController
otherVC.modalPresentationStyle = .overCurrentContext
otherVC.view.center = vc.view.center
otherVC.delegate = self //Create a delegate so that you can control actions such as "OK" buttons
self.view.isUserInteractionEnabled = false //Stop the user interacting with the view controller behind the alert
self.present(otherVC, animated: true, completion: nil)

      

Edit: You can do delegate management actions like closing the alert with actions. For example, this could be an example of your delegate:



protocol AlertDelegate {
    func didCancel()
    func didOkay()
} 

      

Then you can implement it like this:

class RootViewController: UIViewController, AlertDelegate {
    func didCancel() { ... }
    func didOkay() { ... }

    func showAlert() {
        ...
        otherVC.delegate = self
        ...
    }
}

      

Then, in your Alert View Controller, you can interact with the delegate.

class MyAlert: UIViewController {
    var delegate: AlertDelegate!

    @IBAction func cancelButton(sender: UIButton) {
        delegate.didCancel()
        self.dismiss(animated: true, completion: nil)
    }
}

      

So when the cancel button is clicked on the alert view controller, the delegate will say it's canceled and the modal view controller will be rejected. The root view controller will then receive this action and can handle it accordingly.

+2


source


You can use UIAlertViewController

to represent a lot of lines and it will handle scrolling automatically. But the question is, can it be used and good enough?

Take a look at this simple example:

    @IBAction func showAlert(_ sender: UIButton) {        
        var lines = [String]()
        for i in 1...100 {
            lines.append("this is line \(i)")
        }

        let alertController = UIAlertController(title: "title", message: lines.joined(separator: "\n"), preferredStyle: .alert)

        alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))

        present(alertController, animated: true, completion: nil)
    }

      

Which gives me this opinion:

viewing warnings with many lines



with scrolling content.

Several problems and considerations:

  • There seems to be an upper limit. If I change this to for i in 1...500

    , for example, I cannot see any content
  • This is actually not very user friendly.

So ... I think you should consider a different solution than UIAlertViewController

:)

Hope it helps.

+1


source







All Articles