gpt4 book ai didi

ios - 添加 UIPickerView 作为 subview ,很奇怪

转载 作者:行者123 更新时间:2023-11-28 08:30:58 25 4
gpt4 key购买 nike

最近我需要一个解决方案来显示一个类似于 present 的 pickerView,用于为某些属性(如配置文件)选择数据。我所做的是1、创建一个空项目2,创建一个将用作 subview 的AViewController。我在该 View 中添加了一个 pickerView 和一个导航栏,并将 View 的高度调整到 260 左右。3、在那个AViewController的类中写代码。

class APickerViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet weak var pickerView:UIPickerView!
let dataList = [["a","b","c","d"]]

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
//pickerView.dataSource = self
//pickerView.delegate = self
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return dataList.count
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dataList[component].count
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return dataList[component][row]
}

4、在Main ViewController中编写代码,将AViewController的View添加为子view。

class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

let pickerViewController = storyboard?.instantiateViewController(withIdentifier: String(APickerViewController.self)) as! APickerViewController

self.view.addSubview(pickerViewController.view)
self.view.bringSubview(toFront: pickerViewController.view)

pickerViewController.view.frame = CGRect(x: 0, y: self.view.frame.height - 300, width: self.view.frame.width, height: 300)



}

还有一个问题,实际上我不太确定其中的区别。答:

let pickerViewController = storyboard?.instantiateViewController(withIdentifier: String(APickerViewController.self)) as! APickerViewController

乙:

let pickerViewController = APickerViewController()

A 和 B 有什么不同?如果我通过 Interface Builder 做一些 @IBOutlet 设置会受到影响还是什么?

5,去……感到奇怪……

函数

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return dataList[component].count
}

有时会被多次调用,有时不会被调用。

并且数据没有显示在 pickerView 中,有时只显示第 1,2 行而没有其他行,如果我向上或向下滑动,那些 1,2 行数据就会消失。

如果有人有使用 pickerView 作为选项弹出窗口的更好解决方案?

帮助。

谢谢

最佳答案

class EditViewController: UIViewController,UITextFieldDelegate,UIPickerViewDelegate{ 

var picker : UIPickerView = UIPickerView()
var toolBar : UIToolbar = UIToolbar()
@IBOutlet var txtFieldState: UITextField!
var stateArray = ["Andaman & Nicobar Island","Andhra Pradesh","Arunachal Pradesh","Assam","Bihar","Chandigarh","Chhattisgarh","Dadra & Nagar"]
override func viewDidLoad() {
super.viewDidLoad()
txtFieldState.delegate = self
}
func textFieldDidBeginEditing(textField: UITextField) {
if textField == txtFieldState{
txtFieldState.inputView = picker
txtFieldState.inputAccessoryView = toolBar
self.configurePicker()
}
}

func configurePicker()
{
picker.alpha = 1.0
picker.backgroundColor = UIColor(red:241/255.0, green:241/255.0, blue:241/255.0, alpha: 1.0)
picker.showsSelectionIndicator = true
picker.delegate = self
picker.selectedRowInComponent(0)
let screenRect = self.view.frame
let pickerSize = picker.sizeThatFits(CGSizeZero)

let x = screenRect.origin.x + (screenRect.size.width / 2) - (pickerSize.width / 2)

let pickerRect = CGRectMake(x,
screenRect.origin.y + (screenRect.size.height) - (pickerSize.height),
pickerSize.width,
pickerSize.height)

picker.frame = pickerRect

let toolbarSize = toolBar.sizeThatFits(CGSizeZero)
toolBar.frame = CGRectMake(x,pickerRect.origin.y, // right under the picker
pickerSize.width, // make them the same width
toolbarSize.height)

}
func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int){
dropState = row
txtFieldState.text = "\(stateArray[row])"
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
return stateArray[row]
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{

return stateArray.count
}

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int
{
return 1
}
}

关于ios - 添加 UIPickerView 作为 subview ,很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38895686/

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