title: Swift 5: How can get the selected PickerView value inside a UIAlert Textfield?
body: <p>I'm struggling to implement a UIPickerView inside the UIAlert as an input for the textfield. Here's what I've got (think of a simple TODO app);</p>
  <pre><code>import UIKit
  
  class ViewController: UIViewController {
      
      let todoItemCategory = [&quot;Work&quot;, &quot;Home&quot;, &quot;Friends&quot;, &quot;Buy stuff&quot;]
      let pickerView = UIPickerView()
  
      override func viewDidLoad() {
          super.viewDidLoad()
          
          pickerView.dataSource = self
          pickerView.delegate = self
      }
  
      @IBAction func addItemButtonPressed(_ sender: Any) {
          let alert = UIAlertController(title: &quot;Add a new todo item&quot;, message: &quot;Provide the title and the category of the new item.&quot;, preferredStyle: .alert)
          
          alert.addTextField { field in
              field.placeholder = &quot;Type the item title&quot;
              field.returnKeyType = .next
          }
          alert.addTextField { field in
              field.placeholder = &quot;Select category&quot;
              
              field.inputView = self.pickerView
          }
          
          alert.addAction(UIAlertAction(title: &quot;Cancel&quot;, style: .cancel, handler: nil))
          alert.addAction(UIAlertAction(title: &quot;Save&quot;, style: .default, handler: { _ in
              guard let fields = alert.textFields, fields.count == 2 else {
                  return
              }
              let itemTitleField = fields[0]
              let categoryField = fields[1]
              
              guard let itemTitle = itemTitleField.text, !itemTitle.isEmpty, let category = categoryField.text, !category.isEmpty else {
                  print(&quot;Invalid entries&quot;)
                  return
              }
              
              print(itemTitle)
              print(category)
              
          }))
          
          present(alert, animated: true)
      }
  }
  
  //MARK: - Pickerview datasource &amp; delegate methods
  
  extension ViewController: UIPickerViewDataSource {
      func numberOfComponents(in pickerView: UIPickerView) -&gt; Int {
          return 1
      }
      
      func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -&gt; Int {
          return todoItemCategory.count
      }
      
      func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -&gt; String? {
          return todoItemCategory[row]
      }
      
      
  }
  
  extension ViewController: UIPickerViewDelegate {
      func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
          // Update the textfield inside the UIAlert
      }
  }
  
  </code></pre>
  <p>What I've done is;</p>
  <ul>
  <li>A UIAlert view pops up when the user clicks on the button (addItemButtonPressed).</li>
  <li>In the &quot;Select category&quot; textfield, I linked to the PickerView as an input view. The Pickerview with possible categories will then appear as selectable options.</li>
  </ul>
  <p>The picker view appears nicely, but whenever the user selects one of the values, nothing happens. I want the textfield to show the selected category, and I know I need to do something inside</p>
  <pre><code>    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
          // Update the textfield inside the UIAlert
      }
  </code></pre>
  <p>I was going to assign the selected value to a global variable, let's say currentSelectedCategory, but I do not know how to make the UIAlert textfield to read it up again whenever the value gets changed.</p>
  <p>Appreciate your help in advance to this poor swift newbie!</p>