gpt4 book ai didi

swift - 如何在 uialertview swift 3 中添加 uipickerview

转载 作者:行者123 更新时间:2023-11-28 15:28:11 24 4
gpt4 key购买 nike

Swift 3 iOS 10:我想在 UIAlertView 中添加 UIPickerView 以便我可以选择项目并将其值设置为 textField。我可以用 UIView 做到这一点,但我想以更好、更自然的方式改进它。是否可以这样做或有什么出路?

提前致谢!

这是我做的,

Pix1:我的文本框:https://z-p3-scontent.fpnh5-1.fna.fbcdn.net/v/t1.0-9/19702221_1969127863363332_1282515913735651533_n.jpg?oh=ece774626e6691c531dc69b168104fcc&oe=59D72E14

Pix2:我的警报: https://z-p3-scontent.fpnh5-1.fna.fbcdn.net/v/t1.0-9/19665630_1969127860029999_9139151345242136168_n.jpg?oh=d56be1efc3bd772ff68e7e45fa7ebfae&oe=5A0F290F

最佳答案

虽然 Apple 建议我们不要这样做,但您确实可以将选择器 View 添加到您的警报 View Controller 。 UIAlertViewController 除了一个普通的 View Controller 之外没有任何神奇之处。您需要做的就是显示带有大量新行的标题文本(即“选择\n\n\n\n\n\n\n\n\n\n\n\n”)以增加alert高度,然后调用alertViewController.addSubview(datePicker)

然而,问题是为什么您需要编写额外的代码来操作警报 View Controller 并处理要设置值的文本字段?这是另一种更易于维护和实现的方法。

首先创建您的选择器 View 并对其进行配置。

let picker = UIPickerView()
picker.delegate = self
picker.dateSource = self
// configure

然后对于您的每个文本字段,只需调用它

tf1.tag = 1
tf2.tag = 2
tf3.tag = 3

tf1.delegate = self
tf2.delegate = self
tf3.delegate = self

tf1.inputView = picker
tf2.inputView = picker
tf3.inputView = picker

不在您的文本字段委托(delegate)方法中,存储用户单击的文本字段并重新加载选择器 View 数据

var tfTag = 0

func textFieldDidBeginEditing(_ textField: UITextField) {
tfTag = textField.tag
picker.reloadAllComponents()
}

最后,在所有选择器 View 数据源或委托(delegate)方法中,根据每个文本字段的唯一标签号加载不同的数据集

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if tfTag == 0 {
if component == 0 {
return "1"
}else if component == 1 {
return "2"
}else if component == 2 {
return "3"
}else{
return "4"
}
}else if tfTag == 1 {
//...
}else{
//...
}
}

关于swift - 如何在 uialertview swift 3 中添加 uipickerview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44959663/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com