ios 使用swift制作下拉列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30319718/
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 06:00:34  来源:igfitidea点击:

Making a drop down list using swift?

iosswiftdrop-down-menu

提问by mazinAlmas

What is the library to make drop down menu in swift? I am new to Xcode and the Swift language, so can anyone please direct me on how to implement the drop down list in swift?

什么是快速制作下拉菜单的库?我是 Xcode 和 Swift 语言的新手,所以有人可以指导我如何在 swift 中实现下拉列表吗?

采纳答案by Chackle

A 'drop down menu' is a web control / term. In iOS we don't have these. You might be better looking at UIPopoverController. Check out this tutorial for a bit of an insight to PopoverControllers

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

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

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

回答by Charles Xavier

(Swift 3) Add text box and uipickerview to the storyboard then add delegate and data source to uipickerview and add delegate to textbox. Follow video for assistance https://youtu.be/SfjZwgxlwcc

(Swift 3) 将文本框和 uipickerview 添加到故事板,然后将委托和数据源添加到 uipickerview 并将委托添加到文本框。关注视频寻求帮助 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"]

    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 don't want the users to se the keyboard type:

            textField.endEditing(true)
        }
    }
}

回答by Ilan Kutsman

Unfortunately if you're looking to apply UIPopoverControllerin iOS9, you'll get a deprecated class warning. Instead you need to set your desired view's UIModalPresentationPopoverproperty to achieve the same result.

不幸的是,如果您想UIPopoverController在 iOS9 中申请,您将收到已弃用的类警告。相反,您需要设置所需视图的 UIModalPresentationPopover属性才能获得相同的结果。

Popover

In a horizontally regular environment, a presentation style where the content is displayed in a popover view. The background content is dimmed and taps outside the popover cause the popover to be dismissed. If you do not want taps to dismiss the popover, you can assign one or more views to the passthroughViews property of the associated UIPopoverPresentationController object, which you can get from the popoverPresentationController property.

In a horizontally compact environment, this option behaves the same as UIModalPresentationFullScreen.

Available in iOS 8.0 and later.

弹出框

在水平规则的环境中,在弹出视图中显示内容的呈现样式。背景内容变暗,点击弹出框外会导致弹出框消失。如果您不想点击关闭弹出框,您可以将一个或多个视图分配给关联的 UIPopoverPresentationController 对象的 passthroughViews 属性,您可以从 popoverPresentationController 属性中获取该属性。

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

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

Reference: https://developer.apple.com/documentation/uikit/uiviewcontroller/1621355-modalpresentationstyle

参考:https: //developer.apple.com/documentation/uikit/uiviewcontroller/1621355-modalpresentationstyle

回答by Dana Young

You have to be sure to use UIPickerViewDataSource and UIPickerViewDelegate protocols or it will throw an AppDelegate error as of swift 3

您必须确保使用 UIPickerViewDataSource 和 UIPickerViewDelegate 协议,否则它会从 swift 3 开始抛出 AppDelegate 错误

Also please take note of the change in syntax:

另外请注意语法的变化:

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int

is now:

就是现在:

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

The following below worked for me.

以下内容对我有用。

import UIkit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

    @IBOutlet weak var textBox: UITextField!
    @IBOutlet weak var dropDown: UIPickerView!

    var list = ["1", "2", "3"]

    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 don't want the users to se the keyboard type:

            textField.endEditing(true)
        }
    }
}

回答by Durai Amuthan.H

Using UIPickerviewis the right way to go to implement it according to Apple's Human Interface Guidelines

根据 Apple 的人机界面指南,使用UIPickerview是实现它的正确方法

If you select drop down in mobile safari it will show UIPickerviewto let the use choose drop down items.

如果您在移动 safari 中选择下拉菜单,它将显示UIPickerview以让用户选择下拉项目。

Alternatively

或者

you can use UIPopoverControllertill iOS 9 as its deprecated but its better to stick with UIModalPresentationPopoverof view you want o show as well

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

you can use UIActionsheetto show the items but it's better to use UIAlertViewControllerand choose UIActionSheetstyleto show as the former is deprecated in latest versions

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