2010-12-17 168 views
1

我有一个工具栏,我想发布一个textField。我试着用下面的代码,但它不起作用:如何将textField添加到工具栏

UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:@"Item" style:UIBarButtonItemStyleBordered target:self action:@selector(action:)]; 

UITextField *customItem1 = [[UITextField alloc] init]; 

    NSArray *items = [NSArray arrayWithObjects: customItem, customItem1, nil]; 
[self setToolbarItems:items animated:YES]; 

回答

4

工具栏项目必须都是UIBarButtonItems。为了显示别的东西,你嵌入一个视图到项目:

UIBarButtonItem *customItem1 = [[UIBarButtonItem alloc] initWithCustomView:view]; 
//view is the embedded view, in your case a UITextField 
+0

看起来很好,但是我不知道如何的UITextField转换为一个观点:S它现在不崩溃,它现在才寿显示按钮 – k20 2010-12-17 04:54:17

+2

设置文本字段框架并将此文本字段添加为initWithCustomView作为参数。不要担心类型转换。 – sudesh 2010-12-17 05:09:38

+0

非常感谢它很棒! – k20 2010-12-17 05:46:49