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

问题描述

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



我有一些解决方案,如使用 UITableView 用于显示文本项的列表。 (在我的情况下,列表可以是静态的以及动态,所以 UITableView 似乎是一个很好的选择我的情况下)。但我有一件事我不能弄清楚是如何解除下拉...



让我们说这个下拉列表打开某个视图中的某处(让我们说这个视图占据完整的屏幕)。下拉菜单一旦打开,应该在我点击视图中的其他地方时关闭(关闭),就像桌面环境中典型的下拉菜单一样。我如何做?



一种方法是在视图上收听 touchesBegan 事件,是开放的 - 这是很好,但问题是如果我有类似按钮的事情,当用户点击其中一个,然后我没有收到 touchesBegan 在视图上的输入。 / p>

如何解决这个问题?

解决方案

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



您通常还需要一个UIToolbar持有一个完成按钮作为输入附件视图,这显示在选择器上方,并允许您一旦选择, 不要自动。



您可以通过向选择器视图委托方法或操作发送 resignFirstResponder 方法你的完成按钮。



您可以将辅助视图创建为以下工具栏:

  attachmentView = [[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:


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

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?

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?

解决方案

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.

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.

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;

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

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

10-26 18:32