1

How do I create a number picker with a minimum and a maximum defined by a variable?

In my Android application, I created an AlertDialog with a picker number, and the maximum number is linked to a value from my database.

Is it possible to do the same thing in Swift, or something close to it?

My alert controller code is:

func createAlertCarte1(Title:String, Message:String) {
        let alertCarte1 = UIAlertController(title: Title, message: Message, preferredStyle: UIAlertController.Style.alert)
        alertCarte1.addAction(UIAlertAction.init(title: "Ok", style: UIAlertAction.Style.default, handler: { (action1) in
            alertCarte1.dismiss(animated: true, completion: nil)
            self.displayAd()
        }))
        alertCarte1.addAction(UIAlertAction.init(title: "Annuler", style: UIAlertAction.Style.cancel, handler: { (action1) in
            alertCarte1.dismiss(animated: true, completion: nil)
        }))

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

    }
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
  • Does this answer your question? [How to add UIPickerView in UIAlertController?](https://stackoverflow.com/questions/30022486/how-to-add-uipickerview-in-uialertcontroller) – rbaldwin May 10 '20 at 08:44
  • @rbaldwin Yes it can be useful to me, but I am mainly looking for a way to define the maximum number of the NumberPicker with a variable. – Victor Appercé May 10 '20 at 08:50

1 Answers1

9
import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    var pickerView: UIPickerView!
    var pickerData: [Int]!

    override func viewDidLoad() {
        super.viewDidLoad()
        pickerView = UIPickerView(frame: CGRect(x: 10, y: 50, width: 250, height: 150))
        pickerView.delegate = self
        pickerView.dataSource = self

        // This is where you can set your min/max values
        let minNum = 1
        let maxNum = 10
        pickerData = Array(stride(from: minNum, to: maxNum + 1, by: 1))
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        showAlert()
    }

    func showAlert() {
        let ac = UIAlertController(title: "Picker", message: "\n\n\n\n\n\n\n\n\n\n", preferredStyle: .alert)
        ac.view.addSubview(pickerView)
        ac.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
            let pickerValue = self.pickerData[self.pickerView.selectedRow(inComponent: 0)]
            print("Picker value: \(pickerValue) was selected")
        }))
        ac.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        present(ac, animated: true)
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return pickerData.count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return "\(pickerData[row])"
    }
}

enter image description here

rbaldwin
  • 4,581
  • 27
  • 38
  • 1
    Oh thank you very much, I didn't ask for so much! :) but I have one last question, for the (message :) what is \ n for? – Victor Appercé May 10 '20 at 10:02
  • 2
    `\n` is new line - It's a hack to create some space for the picker by adding blank lines. There may be a better way of doing it. – rbaldwin May 10 '20 at 10:04