iOS如何实现下拉列表以及如何关闭它? [英] iOS how to implement a drop down list and how to take care of closing it?

查看:26
本文介绍了iOS如何实现下拉列表以及如何关闭它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些关于如何在 iOS 中实现下拉列表功能的输入.

I need some inputs on how to implement dropdown list kind of functionality in iOS.

我有一些解决方案,比如使用 UITableView 来显示文本项列表.(在我的情况下,列表可以是静态的也可以是动态的,所以 UITableView 似乎是我的情况的一个不错的选择).但我无法弄清楚的一件事是如何关闭下拉菜单...

I have some solutions in mind like using UITableView for displaying the list of text items. (in my case the list could be static as well as dynamic so UITableView seems to be a good option for my case). But one thing I am not able to figure out is how to dismiss the dropdown...

假设在视图的某处打开了这个下拉列表(假设这个视图占据了整个屏幕).下拉菜单一旦打开,当我点击视图中的其他地方时应该会被关闭(关闭),就像典型的下拉菜单在桌面环境中的工作方式一样.我该怎么做?

Let's say there is this dropdown list open somewhere in a view (let's say this view occupies the complete screen). The dropdown, once opened, should get dismissed (closed) when I tap somewhere else in the view, like the way a typical dropdown works in desktop environment. How do I do that?

一种方法是在视图上监听 touchesBegan 事件并查看下拉列表是否打开 - 这很好,但问题是如果我有按钮之类的东西,当用户点击其中一个时我在视图上没有收到 touchesBegan 输入.

One way is to listen to touchesBegan events on the view and see if the dropdown is open -this is fine but problem is if I have things like button and when user clicks on one of them then I don't receive the touchesBegan input on the view.

我如何以通用方式解决这个问题?

How do I go about solving this in a generic way?

推荐答案

下拉列表通常在 iOS 中使用 UIPickerView 实现.选择器视图可以设置为文本字段的输入视图,它将保持下拉菜单,然后以与键盘相同的方式在屏幕上和屏幕外进行动画处理.

Drop down lists are usually implemented in iOS using a UIPickerView. The picker view can be set as the input view of the text field which would hold the drop down, it is then animated on and off screen in the same manner as the keyboard.

您通常还需要一个包含完成"按钮的 UIToolbar 作为输入附件视图,它出现在选择器上方,如果您没有自动执行选择,则一旦做出选择,您就可以关闭.

You usually also need a UIToolbar holding a "Done" button as the input accessory view, this appears above the picker and allows you to dismiss once a choice is made if you're not doing that automatically.

您可以通过将 resignFirstResponder 发送到文本字段来移除选择器,无论是从选择器视图委托方法还是完成按钮的操作方法.

You remove the picker by sending resignFirstResponder to the text field, either from a picker view delegate method or the action method of your done button.

您将工具栏创建为附件视图,如下所示:

You create the toolbar as an accessory view as follows:

accessoryView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
accessoryView.barStyle = UIBarStyleBlackTranslucent;

UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTapped:)];

accessoryView.items = [NSArray arrayWithObjects:space,done, nil];

textField.inputAccessoryView = accessoryView;

这将在右侧为您提供一个完成"按钮,该按钮连接到名为 doneTapped 的操作方法:

This will give you a single "Done" button on the right which is connected to an action method called doneTapped:

这篇关于iOS如何实现下拉列表以及如何关闭它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆