UITextField with a dropdown menu for iPhone

2019-04-12 06:16发布

问题:

Is there an easy way to create a drop down menu for a UITextField, so that the user can type a custom entry or select one from a list?

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

Just do this:

Add this to main view when user clickes on UITextField.

[self.view addSubView:TableView];