Swift 5:如何在 UIAlert Textfield 中获取选定的 PickerView 值?

Swift 5: How can get the selected PickerView value inside a UIAlert Textfield?

我正在努力在 UIAlert 中实现一个 UIPickerView 作为文本字段的输入。这是我得到的(想想一个简单的 TODO 应用程序);

import UIKit

class ViewController: UIViewController {
    
    let todoItemCategory = ["Work", "Home", "Friends", "Buy stuff"]
    let pickerView = UIPickerView()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        pickerView.dataSource = self
        pickerView.delegate = self
    }

    @IBAction func addItemButtonPressed(_ sender: Any) {
        let alert = UIAlertController(title: "Add a new todo item", message: "Provide the title and the category of the new item.", preferredStyle: .alert)
        
        alert.addTextField { field in
            field.placeholder = "Type the item title"
            field.returnKeyType = .next
        }
        alert.addTextField { field in
            field.placeholder = "Select category"
            
            field.inputView = self.pickerView
        }
        
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        alert.addAction(UIAlertAction(title: "Save", 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("Invalid entries")
                return
            }
            
            print(itemTitle)
            print(category)
            
        }))
        
        present(alert, animated: true)
    }
}

//MARK: - Pickerview datasource & delegate methods

extension ViewController: UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return todoItemCategory.count
    }
    
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return todoItemCategory[row]
    }
    
    
}

extension ViewController: UIPickerViewDelegate {
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        // Update the textfield inside the UIAlert
    }
}

我所做的是;

选择器视图显示得很好,但是每当用户选择其中一个值时,什么也没有发生。我希望文本字段显示所选类别,我知道我需要在里面做一些事情

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        // Update the textfield inside the UIAlert
    }

我打算将选定的值分配给一个全局变量,比如说 currentSelectedCategory,但我不知道如何让 UIAlert 文本字段在值发生变化时再次读取它。

提前感谢您对这个可怜的 swift 新手的帮助!

在按钮操作之外设置一个 UIAlertController 对象引用并访问它。

class ViewController: UIViewController {
    
    // Other code
    
    var alert: UIAlertController = UIAlertController()
    
    // Other code
    @IBAction func addItemButtonPressed(_ sender: Any) {
        alert = UIAlertController(title: "Add a new todo item", message: "Provide the title and the category of the new item.", preferredStyle: .alert)
        
        // Other code
    }
}

extension ViewController: UIPickerViewDelegate {
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        // Update the textfield inside the UIAlert
        alert.textFields?.last?.text = todoItemCategory[row]
    }
}