Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add UITextField to UIToolbar

I tried this to add UITextField to a UIToolbar but got a run-time error with reason as: [UITextField view]: unrecognized selector sent to instance ...

[toolbar setItems:[NSArray arrayWithObjects:textField, nil]];

toolbar is an instance of UIToolbar and textField is an instance of UITextField.

like image 216
user523234 Avatar asked Aug 07 '11 18:08

user523234


1 Answers

The items of a UIToolbar have to be of type UIBarButtonItem. Use initWithCustomView: to create a toolbar item that contains your text field.

UIBarButtonItem *textFieldItem = [[UIBarButtonItem alloc] initWithCustomView:textField];
toolbar.items = @[textFieldItem];
like image 98
omz Avatar answered Sep 29 '22 13:09

omz