如何给barbuttonitem行动?

当我在UIToolBar上单击完成按钮时,我想调出“TableViewController”的.nib。 但下面不允许点击显示新视图。 我该如何纠正这个? 请告诉我哪里出错了,应该更换什么以及为什么。

//Here's the selector in my overlay. UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed)]; //Here's how I made my action. Btw, the uitoolbar has no nib, it's an overlay on the //(camera mode). -(void)doneButtonPressed { TableViewController *tableView = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:nil]; tableView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentModalViewController:tableView animated:YES]; } //Yet nothing happens when I click on my done button on my overlay. And I've made sure // i've imported .h frameworks correctly too. 

假设你要从一个位于UItoolbar叠加层上的barbuttonitem中调出一个笔尖。 你会怎么做?

我被告知要使其正常运行,我必须添加[barButtonItem addTarget:self action:@selector(doneButtonPressed)forControlEvents:UIControlEventTouchUpInside]; 。

但如果我添加它我得到这个:

 UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone addTarget:self action:@selector(doneButtonPressed) forControlEvents:UIControlEventTouchUpInside]; 

这导致我在读取“实例方法”时出错 – initWithBarButtonSystemItem:target:action:forControlEvents:’not found(返回类型默认为’id’)“

除了向我展示正确的添加剂,除了我在这里写的代码之外,请告诉我解决方案。

如果您使用的是XCode 4,则只需按Ctrl +拖动BarButtonItem .h文件,然后您可以自动创建IB动作。

UIBarButtonItem遵循默认的UIControlEventTouchUpInside ,您无法设置它。 Xcode应该自动建议分配它的方法,但正确的代码是:

 UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed)]; 

注意,没有forControlEvents: .

尝试这些更改:

 UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)]; -(void)doneButtonPressed:(id)sender { } 

目标传递给发起动作的对象(即UIBarButtonItem本身)

其次,在doneButtonPressed函数中设置断点。 例如,如果tableView设置为nil,那么你将看不到任何结果。 即,实例化此视图控制器可能存在问题。