Cocoa touch Can';t将uibarbuttonite添加到独立的UINavigationBar

Cocoa touch Can';t将uibarbuttonite添加到独立的UINavigationBar,cocoa-touch,uinavigationbar,Cocoa Touch,Uinavigationbar,我正在通过编程向UIView添加UINavigationBar,现在需要向其添加UIBarButtonim。我尝试使用以下方法: UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissView)]; [header setItems:[NSArray arrayW

我正在通过编程向UIView添加UINavigationBar,现在需要向其添加UIBarButtonim。我尝试使用以下方法:

UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissView)];
[header setItems:[NSArray arrayWithObjects:doneButton, nil] animated:NO];
[doneButton release];
我的应用程序崩溃,我在控制台中发现:

***由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[UIBarButtonim setNavigationBar:]:无法识别的选择器发送到实例0x4b75c00'

如果有人能指出我在这里做错了什么,我将不胜感激

谢谢。
Ricky.

UINavigationBar
接受一个对象数组,每个对象都包含关于给定导航层次结构级别的属性。您可能希望创建一个新的
UINavigationItem
,然后将其
rightBarButtonItem
属性设置为“完成”按钮。

您不太可能需要创建一个新的
UINavigationItem
。相反,如果您已经有一个
UINavigationBar
从一个包含视图的nib初始化,您可以将
UINavigationItem
添加到
UINavigationBar
topItem
属性中。大概是这样的:

UIBarButtonItem *closeBtn = [[UIBarButtonItem alloc] initWithTitle:@"Close" 
                                                             style:UIBarButtonItemStyleBordered 
                                                            target:self 
                                                            action:@selector(closeBtnPressed)];
self.navigationBar.topItem.leftBarButtonItem = closeBtn;
[closeBtn release];