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



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

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
}
}

我所做的是;

  • 当用户点击按钮(addItemButtonPressed)时,会弹出一个UIAlert视图。
  • 在"选择类别"中textfield,我链接到PickerView作为输入视图。Pickerview可能类别将作为可选择的选项出现。

选择器视图显示得很好,但是每当用户选择其中一个值时,什么都没有发生。我想让textfield显示所选的类别,我知道我需要在

里面做点什么
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
// Update the textfield inside the UIAlert
}

我要分配选定的值给一个全局变量,让我们说currentSelectedCategory,但我不知道如何使UIAlert textfield读取它再次每当值被改变。

提前感谢你对这个可怜的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]
}
}

最新更新