ios 以编程方式创建和显示 UIPickerView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24836369/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:27:21  来源:igfitidea点击:

Programmatically Create and Show UIPickerView

iosswiftuipickerview

提问by trumpeter201

I am attempting to create a UIPickerView programmatically and display it as the firstResponder of a textfield, however, the picker view is not showing up. textField is connected to an object in the interface builder, but pickerView is being created programatically.

我试图以编程方式创建 UIPickerView 并将其显示为文本字段的 firstResponder,但是,选择器视图没有显示。textField 连接到界面构建器中的一个对象,但 pickerView 正在以编程方式创建。

class View: UIViewController {
    @IBOutlet var picker : UIPickerView = UIPickerView.alloc()
    @IBOutlet var textField : UITextField = nil
    override func viewDidLoad() {
        super.viewDidLoad()
        picker = UIPickerView()
        picker.delegate = self
        picker.dataSource = self
        picker.backgroundColor = UIColor.blackColor()
        textField.inputView = picker
    }
}
extension View: UIPickerViewDataSource {

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

    func pickerView(pickerView: UIPickerView!, numberOfRowsInComponent component: Int) -> Int {
        return 5
    }
}

extension View: UIPickerViewDelegate {

    func pickerView(pickerView: UIPickerView!, titleForRow row: Int, forComponent component: Int) -> String!
    {
        return "Text"
    }
}

Why can't I see this pickerView when I run the app?

为什么我在运行应用程序时看不到这个pickerView?

Edit: Adding a breakpoint inside the extensions does not stop the program, they are not being called.

编辑:在扩展中添加断点不会停止程序,它们不会被调用。

采纳答案by trumpeter201

I found the problem-the code for assigning the input view doesn't include self. It should read

我发现了问题-分配输入视图的代码不包含 self. 它应该读

self.textField.inputView = picker

回答by Jason Wiles

I was having the same problem trying to get the picker view to show up when clicking in the textfield. My issue was that for some reason my iOS simulator had the "Connect Hardware Keyboard" checked. In the iOS menu go to Hardware -> Keyboard and make sure "Connect Hardware Keyboard" is unchecked. Feels dumb now not noticing any keyboard was popping up in the app for several hours but hopefully this will help save someone else the frustration.

我在尝试在文本字段中单击时显示选择器视图时遇到了同样的问题。我的问题是由于某种原因,我的 iOS 模拟器检查了“连接硬件键盘”。在 iOS 菜单中,转到硬件 -> 键盘并确保未选中“连接硬件键盘”。现在感觉很愚蠢,几个小时内没有注意到应用程序中弹出任何键盘,但希望这将有助于避免其他人的挫败感。

Just wanted to add an edit: In the iOS simulator you can try toggling the software keyboard (command+K) This worked for me as well in this case and allowed me to keep the hardware keyboard connected. Just something to check quickly before assuming your code is incorrect.

只是想添加一个编辑:在 iOS 模拟器中,您可以尝试切换软件键盘(command+K) 这在这种情况下对我也有效,并允许我保持硬件键盘的连接。只是在假设您的代码不正确之前需要快速检查。

回答by Louis Zhu

I'm not sure why you cannot see the picker. But it's a wrong way.

我不知道为什么你看不到选择器。但这是错误的方式。

To create an instance using:

要使用以下方法创建实例:

 picker = UIPickerView.alloc()

In Swift:

在斯威夫特:

you should use:

你应该使用:

picker = UIPickerView()