5

当在我的 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 中调出一个 nib。你会怎么做?

有人告诉我,要使其正常运行,我必须添加 [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 (return type defaults to 'id')

除了我在这里编写的代码之外,请向我展示解决方案,而不是只向我展示正确的添加剂。

4

3 回答 3

5

如果你使用 XCode 4,你可以简单地 Ctrl+DragBarButtonItem到你的 .h 文件,然后你可以从那里自动创建一个 IB Action。

于 2012-08-01T18:41:30.183 回答
1

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

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

注意,没有forControlEvents:.

于 2012-08-01T19:09:21.550 回答
1

尝试以下更改:

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

-(void)doneButtonPressed:(id)sender
{
}

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

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

于 2012-08-01T19:58:51.077 回答