如何在 xcode 中创建 DropDown?

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

How to create DropDown in xcode?

iphonexcode

提问by Adnan Khan

I am a very new developer for IOS, i need help, How to create dropdown box in xcode, any one provide me example to create country list in drop down?

我是一位非常新的 IOS 开发人员,我需要帮助,如何在 xcode 中创建下拉框,有人提供我在下拉列表中创建国家/地区列表的示例吗?

回答by Nitin Gohel

Here I found two demos for dropDown list, One is creating custom expandable UITableViewCelllike :-

在这里,我找到了两个下拉列表演示,一个是创建自定义扩展,UITableViewCell如:-

enter image description here

在此处输入图片说明

to

enter image description here

在此处输入图片说明

source code :- DEMO

源代码:-演示

ANDAnother is custom Drop Down list like:-

另一种是定制的下拉列表中,如: -

enter image description here

在此处输入图片说明

by clicking of test then open drop down list as bellow like image

通过点击测试然后打开下拉列表如下图

drop down list

下拉列表

source code with Tab Bar:-DEMO

带有标签栏的源代码:- DEMO

updated source code without Tab Bar :-

没有标签栏的更新源代码:-

http://www.sendspace.com/file/83qws5

http://www.sendspace.com/file/83qws5

回答by MANIAK_dobrii

I beleive you shouldn't use dropdown boxes in iOS, as it's a desktop OS UI control element. You should think up something else using existing components (like PickerView), that's the words for UI consistency.

我相信你不应该在 iOS 中使用下拉框,因为它是一个桌面操作系统 UI 控制元素。您应该使用现有组件(如 PickerView)想出一些其他的东西,这就是 UI 一致性的话。

And if you need this anyway, you may create a table view, place it beneath your label and a triangular button(which causes it to appear and disappear) and populate it with values.

如果你无论如何都需要这个,你可以创建一个表格视图,将它放在你的标签和一个三角形按钮下面(这会导致它出现和消失)并用值填充它。

回答by tGilani

Since there are no native DropDownelements in iOS, you could make use of a TextFieldwith custom background and a UITableViewto accomplish that. Here is how to go about it.

由于DropDowniOS中没有本机元素,因此您可以使用TextField具有自定义背景的 aUITableView来完成此操作。这是如何去做的。

Pseudocode

伪代码

  • Create a TextFieldand set it's delegate to parent controller
  • Implement UITextFieldDelegateand implement the textFieldShouldBeginEditingmethod
  • Create a new UIViewControllerand implement UITableViewin it programmatically.
  • Create a custom protocol and create it's object (delegate) it.
  • In textFieldShouldBeginEditingmethod, load this controller and present it modally passing the required table's data source and set delegate as parent.
  • in the new tableViewController, implement UITableViewDelegateand implement the didSelectRowAtIndex path method.
  • Upon row selection, call the delegate with passing appropriate data.
  • dismiss the modally presented controller.
  • 创建一个TextField并将其委托给父控制器
  • 实现UITextFieldDelegate和实现textFieldShouldBeginEditing方法
  • 创建一个新的UIViewControllerUITableView以编程方式在其中实施。
  • 创建一个自定义协议并创建它的对象(委托)它。
  • textFieldShouldBeginEditing方法中,加载此控制器并以模态方式呈现它,传递所需表的数据源并将委托设置为父级。
  • 在 new 中tableViewController,实现UITableViewDelegate并实现 didSelectRowAtIndex 路径方法。
  • 选择行后,通过传递适当的数据调用委托。
  • 关闭模态呈现的控制器。

回答by IronManGill

The easy and simple way to design a drop down list is by representing it like a UITableViewand some animation. This makes it look really like a dropdownlist. Here is a code I used for creating one . For this first import the < QuartzCore/QuartzCore.h > framework.

设计下拉列表的简单方法是将其表示为一个UITableView动画。这使它看起来很像一个下拉列表。这是我用来创建一个的代码。为此,首先导入 < QuartzCore/QuartzCore.h > 框架。

-(IBAction)DropDownTable:(id)sender
{
TableView.hidden = NO;
if(TableView.frame.origin.y ==203)
{
    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.5f];
    [TableView setFrame:CGRectMake(224, 204, 27, 160)];
    [UIView commitAnimations];
    [self.view TableView];
}

else if (TableView.frame.origin.y == 204)
{
    [TableView setFrame:CGRectMake(224, 203, 27, 0)];
    TableView.hidden = YES;
}

[self.view addSubview:TableActivityLevel];
}

First make a tableview , declare its methods and make the array . Put this function on the click of a UIButtonand youll see it work !!! Happy coding :)

首先创建一个 tableview,声明它的方法并创建数组。把这个功能放在点击 a 上UIButton,你会看到它起作用了!!!快乐编码:)

回答by djdance

just for whom searching for small simple swift combo boxhere in 2016 year, i've tried a few of old and new (but obj-c) libs, and at last selected this:

只是为了那些在 2016 年在这里寻找小型简单swift 组合框的人,我尝试了一些新旧(但 obj-c)库,最后选择了这个:

https://github.com/sw0906/SWCombox

https://github.com/sw0906/SWCombox

here is screenshot: enter image description here

这是截图: 在此处输入图片说明