A. Fort
A. Fort

Reputation: 11

How can I do an UIPickerView as Textfield inside an Alert

So I am basically trying to create an basic login screen which only pops up during your first launch. This login screen is presented as an alert. Now I want the user beneath the username and password fields to select his "grade", by simply choosing it via pickerview

In order to follow my ideas, here is my code so far:

import UIKit

class ViewController: UIViewController {


    func loginAlert() {

        let loginController = UIAlertController(title: "Login", message: "Please enter your credentials.", preferredStyle: .alert)

        let actionLogin = UIAlertAction(title: "Login", style: .default) { (action:UIAlertAction) in
            //This is called when the user presses the login button.

            let textUser = loginController.textFields![0] as UITextField;

            UserDefaults.standard.set(textUser.text, forKey: "Username")
            //print("Username: \(UserDefaults.standard.value(forKey: "Username")!)")


            let textPW = loginController.textFields![1] as UITextField

            UserDefaults.standard.set(textPW.text, forKey: "Password")
            //print("Password: \(UserDefaults.standard.value(forKey: "Password")!)")

        }

        loginController.addTextField { (textField) in
            //Configure the attributes of the second text box.
            textField.placeholder = "E-mail"
        }

        loginController.addTextField { (textField) in
            //Configure the attributes of the second text box.
            textField.placeholder = "Password"
            textField.isSecureTextEntry = true
        }


        //Add the buttons
        loginController.addAction(actionLogin)

        //Present the alert controller
        if let x = UserDefaults.standard.object(forKey: "Username") as? String {
            if let y = UserDefaults.standard.object(forKey: "Password") as? String {
                print("Username: \(UserDefaults.standard.value(forKey: "Username")!)")
                print("Password: \(UserDefaults.standard.value(forKey: "Password")!)")
            }
            else {
                self.present(loginController, animated: true)
            }
        }
        else {
            self.present(loginController, animated: true)
        }

    }


    override func viewDidAppear(_ animated: Bool) {

        // Do any additional setup after loading the view, typically from a nib.
        loginAlert()

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

With this code it should look like this:

enter image description here

so back to my question: underneath the username and password textfields, there should be another textfield which input you can choose by a pickerview (or only a pickerview if it is easier to handle)

How can I do that?

Upvotes: 1

Views: 976

Answers (2)

A. Fort
A. Fort

Reputation: 11

here are some further news: @DiegoQ made the recommendation of an custom view to manage the login

I did some research and found a suitable youtube video https://www.youtube.com/watch?v=WIaRs2d6Xm0

did pretty much everything to rebuild this scenario - but failed.

so my question now is - is there a way to do it like the owner of the youtube video did and implement the feature I mentioned above?

really looking forward for any kind of help

Upvotes: 0

DiegoQ
DiegoQ

Reputation: 1116

I found a way to do this:

First, you need to crate a var to save a reference of the TextField:

var textFieldPicker: UITextField?

Second, build your textField:

loginController.addTextField { (textField) in
        //Configure the attributes of the second text box.
        textField.placeholder = "Picker"

        // Implement PickerView delegates
        let pickerView = UIPickerView()
        pickerView.dataSource = self
        pickerView.delegate = self

        let toolbar = UIToolbar()
        toolbar.barStyle = UIBarStyle.default
        toolbar.isTranslucent = true

        let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.done, target: self, action: #selector(self.dismissPickerPressed))
        let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
        toolbar.setItems([spaceButton, doneButton], animated: false)
        toolbar.isUserInteractionEnabled = true
        toolbar.sizeToFit()


        textField.inputAccessoryView = toolbar
        textField.inputView = pickerView

        // Save the textField to assign values
        self.textFieldPicker = textField;

 }

Third, Implement the delegates of PickerView

To assign values in the TextField of AlertView, you have to use:

self.textFieldPicker?.text = "Value"

Anyways I recommend you that create a custom View to manage the login

Upvotes: 2

Related Questions