简体   繁体   中英

UITextField with a dropdown menu for iPhone

有没有一种简单的方法可以为UITextField创建下拉菜单,以便用户可以键入自定义条目或从列表中选择一个?

For drop down menu you can implement UITableView. You can populate this UItableview with your list.

Just declare it in ViewDidLoad method of your ViewController and then you can show or hid accordingly later.

    TableView = [[UITableView alloc]initWithFrame:CGRectMake(x,x,x,x) ];
    TableView.delegate = self;
    TableView.dataSource = self;

Add this to main view when user clickes on UITextField.

[self.view addSubView:TableView];

If you want user to select from given list only better option is UIButton instead of UITextField.

EDIT: I didn't notice it is the same as above answer.

You can show a down arrow button besides uitextfield. And add a tableview of width same as textfield and height whatever you want and place it exactly below the textfield and keep it hidden. On click to downarrow button make tableView.hidden = NO so that it will appear as dropdown and again in didSelectRowAtIndexPath function of tableview make it hidden and place the selected row value in textfield. That's it.

What you said is correct and we can also use textfield delegate method to populate the result in the table view, instead of using the down arrow. When the user enters some text in the in the text field, the text field delegate method (didChangeText Delegate Method) will be called, in that method you can update the table view. No need to add a dropdown arrow button in the text field.

Just do this:

Add this to main view when user clickes on UITextField .

[self.view addSubView:TableView];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM