首页 > 解决方案 > 如何在 UIAlertController 中添加 UIPickerView?

问题描述

在此处输入图像描述

UIAlertController用 aUITextfield和 a 创建了 a UIPickerview

当我按下按钮显示 UIAlert 时,UIPickerView它完全被取代了。我该如何解决?

这是我的代码:

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




 func passwordAlert() {
        let alert = UIAlertController(title: "type in some text", message: "", preferredStyle: .alert)
        alert.addTextField { (passwordCategoryTextfield) in
            passwordCategoryTextfield.placeholder = "e.g Business"
        }
        let pickerFrame = CGRect(x: 0, y: 0, width: 250, height: 300)
        let picker = UIPickerView(frame: pickerFrame)
        picker.delegate = self
        picker.dataSource = self
        alert.view.addSubview(picker)
        alert.addAction(UIAlertAction(title: "add", style: .default, handler: { (action) in
            alert.dismiss(animated: true, completion: nil)
            let passwordInfo = alert.textFields?.first?.text
            self.individualPasswordInformaiton.insert("\(passwordInfo ?? "")", at: 0)
            print(self.individualPasswordInformaiton)
        }))

        alert.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: { (action) in
            alert.dismiss(animated: true, completion: nil)

        }))


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

    }

标签: swiftuipickerviewuialertcontroller

解决方案


You cannot modify UIAlertController. Instead, make an ordinary custom presented view controller. Now the interface is up to you. If you like, you can make your presented view controller's look and act just like a UIAlertController's view (as I demonstrate here).


推荐阅读