如何在uialertview swift 3中添加uipickerview(how to add uipickerview in uialertview swift 3)

编程入门 行业动态 更新时间:2024-10-24 18:17:40
如何在uialertview swift 3中添加uipickerview(how to add uipickerview in uialertview swift 3)

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

Swift 3 iOS 10: I want to add UIPickerView in UIAlertView so that i can choose item and set it's value to the textField. I could do it with UIView but I want to improve it in a better and natural way. Is it possible to do this or any way out?

Thank in advance!

This is what I have done,

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

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

最满意答案

尽管Apple建议我们不要这样做,但您确实可以为警报视图控制器添加一个选择器视图。 除了普通的视图控制器之外, UIAlertViewController没有什么神奇之处。 您需要做的就是显示带有许多新行的标题文本(即“选择\ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n \ n”)以增加警报高度,然后调用alertViewController.addSubview(datePicker)

但是问题是为什么你需要编写额外的代码来操作警报视图控制器并处理哪个文本字段来设置值? 这是另一种更容易维护和实现的方法。

首先创建选择器视图并进行配置。

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

不在您的文本字段委托方法中,存储用户单击的文本字段并重新加载选取器视图数据

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

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

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{ //... } }

Although Apple suggests us not to do so, you can indeed add a picker view to your alert view controller. UIAlertViewController has nothing magic other than a normal view controller. All you need to do is to show title text with lots of new lines (namely "Select\n\n\n\n\n\n\n\n\n\n\n\n") in order to increase the alert height, and then call alertViewController.addSubview(datePicker)

However the questions is why you need to write extra code to manipulate an alert view controller and handle which text field to set value? Here is another approach that is way easier to maintain and implement.

First create your picker view and configure it.

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

Then for each of your text field, simply call this

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

Not in your text field delegate method, store which text field user clicked on and reload picker view data

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

Finally in all your picker view data source or delegate method, load different set of data base on the unique tag number from each of the text field

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{ //... } }

更多推荐

本文发布于:2023-07-27 15:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1292408.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   uialertview   swift   uipickerview   add

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!