我正在构建一个应用程序,在顶部导航中调用三个按钮。酒吧。我不想做标签栏,因为标签栏中只有一个项目。
在下面的代码中,左键(Edit)工作正常。右边的按钮-链接到卡片也工作良好。然而愚蠢的添加按钮给了我一个错误,它不能找到选择器(insertNewObject)。
如果我拿出我的自定义代码并使用"Works in a pinch"的代码,那么…它的工作原理。
我希望知道我做错了什么,或者,另一种方法来处理手头的问题-链接到应用程序的另一部分。
TIA。
/*
// THIS WORKS IN A PINCH
// Set up the edit and add buttons.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
// END OF WHAT WORKS
*/
// create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 105, 44.01)];
self.navigationItem.leftBarButtonItem = self.editButtonItem;
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem* addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject)];
addButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:addButton];
[addButton release];
// create a standard "add" button
// create a flip button
UIBarButtonItem* flipButton = [[UIBarButtonItem alloc]
initWithTitle:@"Cards" style:UIBarButtonItemStyleBordered
target:self action:@selector(flipToFront)];
[buttons addObject:flipButton];
[flipButton release];
// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
// and put the toolbar in the nav bar
UIBarButtonItem* rightButtonBar = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = rightButtonBar;
[rightButtonBar release];
选择器名称后可能缺少冒号。我认为只是放入@selector(newObject)是对没有参数的方法的引用。@selector(newObject:)也可以。