swift - 如何在 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)
}
解决方案
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).
推荐阅读
- neo4j - 如何动态更改 neo4j-ogm 或 spring-data-neo4j 中的实体类型?
- javascript - 向正则表达式边界添加例外
- typescript - “(旗帜) ?” 句法。我不明白,也无法通过 Google 搜索找到它
- javascript - 无法使用 click() 函数单击锚标记
- java - 队列 jms [wso2] 消息代理。回拨服务
- c# - 过滤掉两个List的集合差
在 C# 中使用 LINQ - python - 不可变列表属性
- angular - 如何在 Angular 6 中为反应形式正确实现自定义正则表达式验证器?
- mysql - MySql 内部 JSON 插入或更新(如果存在)
- php - 如何将 Youtube iframe 转换为 amp-youtube 标签