使用swift制作下拉列表?

什么是图书馆快速下拉菜单? 我是Xcode和Swift语言的新手,所以任何人都可以请直接指导我如何在swift中实现下拉列表?

“下拉菜单”是一个网页控制/术语。 在iOS中我们没有这些。 你可能更好看UIPopoverController 。 查看本教程,了解PopoverControllers的一些内容

http://www.raywenderlich.com/29472/ipad-for-iphone-developers-101-in-ios-6-uipopovercontroller-tutorial

(Swift 3)将文本框和uipickerview添加到故事板,然后将委托和数据源添加到uipickerview并将委托添加到文本框。 跟随video寻求帮助https://youtu.be/SfjZwgxlwcc

 import UIKit class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate { @IBOutlet weak var textBox: UITextField! @IBOutlet weak var dropDown: UIPickerView! var list = ["1", "2", "3"] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } public func numberOfComponents(in pickerView: UIPickerView) -> Int{ return 1 } public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{ return list.count } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { self.view.endEditing(true) return list[row] } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { self.textBox.text = self.list[row] self.dropDown.isHidden = true } func textFieldDidBeginEditing(_ textField: UITextField) { if textField == self.textBox { self.dropDown.isHidden = false //if you dont want the users to se the keyboard type: textField.endEditing(true) } } } 

不幸的是,如果你想在iOS9中应用UIPopoverController ,你会得到一个弃用的类警告。 相反,您需要设置您所需的视图的UIModalPresentationPopover属性来实现相同的结果。

酥料饼

在横向常规的环境中,在popup视图中显示内容的呈现样式。 背景内容变暗,在popup窗口之外点击导致popup窗口被解除。 如果您不想点击closurespopup窗口,则可以将一个或多个视图分配给关联的UIPopoverPresentationController对象的passthroughViews属性,您可以从popoverPresentationController属性中获取该对象。

在水平紧凑的环境中,此选项的行为与UIModalPresentationFullScreen相同。

在iOS 8.0及更高版本中可用。

参考: https : //developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/#//apple_ref/c/tdef/UIModalPresentationStyle

使用UIPickerview是根据苹果的人机界面指南实施它的正确方法

如果您在移动Safari浏览器中select下拉菜单,则会显示UIPickerview ,让用户select下拉项目。

另外

你可以使用UIPopoverController,直到iOS 9,因为它的弃用,但它更好地坚持使用UIModalPresentationPopover您要显示的视图

您可以使用UIActionsheet来显示项目,但是最好使用UIAlertViewController并selectUIActionSheetstyle来显示,因为前者在最新版本中已被弃用

你必须确保使用UIPickerViewDataSource和UIPickerViewDelegate协议,否则将会抛出一个AppDelegate错误

还请注意语法的变化:

 func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int 

就是现在:

 public func numberOfComponents(in pickerView: UIPickerView) -> Int 

下面的下面为我工作。

 import UIkit class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { @IBOutlet weak var textBox: UITextField! @IBOutlet weak var dropDown: UIPickerView! var list = ["1", "2", "3"] override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } public func numberOfComponents(in pickerView: UIPickerView) -> Int{ return 1 } public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{ return list.count } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { self.view.endEditing(true) return list[row] } func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { self.textBox.text = self.list[row] self.dropDown.isHidden = true } func textFieldDidBeginEditing(_ textField: UITextField) { if textField == self.textBox { self.dropDown.isHidden = false //if you dont want the users to se the keyboard type: textField.endEditing(true) } } }