DEV Community

Avelyn Hyunjeong Choi
Avelyn Hyunjeong Choi

Posted on β€’ Edited on

2

UIAlertController taking multiple user inputs

What if there is more than one user input?

Following program will implement an alert that asks the user for username and password. Then log the credentials in the console.

    @IBAction func alertCalled(_ sender: Any) {
        let alert = UIAlertController(title: "Login", message: "Enter username and password", preferredStyle: .alert)

        alert.addTextField() { textField in
            textField.placeholder = "username"
        }

        alert.addTextField() { textField in
            textField.placeholder = "password"
            textField.isSecureTextEntry = true
        }

        let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
        let yesAction = UIAlertAction(title: "OK", style: .destructive) { _ in
            let usernameTF = alert.textFields![0] as UITextField
            let passwordTF = alert.textFields![1] as UITextField
            guard let username = usernameTF.text, let password = passwordTF.text  else {
                return
            }
            print("Username: \(username) || Password: \(password)")
        }

        alert.addAction(cancelAction)
        alert.addAction(yesAction)

        self.present(alert, animated: true)
    }
Enter fullscreen mode Exit fullscreen mode

result1

result2

Sentry growth stunted Image

If you are wasting time trying to track down the cause of a crash, it’s time for a better solution. Get your crash rates to zero (or close to zero as possible) with less time and effort.

Try Sentry for more visibility into crashes, better workflow tools, and customizable alerts and reporting.

Switch Tools πŸ”

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay