我正在更新一些旧代码,为了在工具栏中腾出更多空间,我将按钮从测试转换为图像。loadView 中的新旧代码示例如下:
// New code, doesn't work.
UIButton *toggleKeyboardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
toggleKeyboardBtn.bounds = CGRectMake( 0, 0, showKeyboardImage.size.width, showKeyboardImage.size.height );
[toggleKeyboardBtn setImage:showKeyboardImage forState:UIControlStateNormal];
UIBarButtonItem *toggleKeyboardItem = [[UIBarButtonItem alloc] initWithCustomView:toggleKeyboardBtn];
[toggleKeyboardItem setTarget:self];
[toggleKeyboardItem setAction:@selector(toggleKeyboard:)];
// Original code, works jut fine.
UIBarButtonItem *setupItem = [[[UIBarButtonItem alloc] initWithTitle:@"Setup" style:UIBarButtonItemStyleBordered target:[UIApplication sharedApplication].delegate action:@selector(showSetupView:)] autorelease];
我的新代码是从Cannot set action on UIBarButtonItem复制的,我很确定我没有犯他们的错误,因为我的文本按钮工作得很好。
showSetupView() 在我的 AppController.m 文件中,按下按钮时设置屏幕出现和消失。
toggleKeyboard(),OTOH,与 loadView() 例程在同一个文件中,当前包含以下代码:
//- (void)toggleKeyboard {
- (IBAction)toggleKeyboard:(id)sender {
NSLog(@"Entering toggleKeyboard()...");
hiddenKeyboard = !hiddenKeyboard;
[self prepareToolbarsAndStatusbar];
}
不用说,虽然我看到了按钮按下动画,但我从来没有看到 NSLog 消息。最后一次观察,是偶然的。将 setAction 选择器更改为:
[toggleKeyboardItem setAction:@selector(noSuchRoutine:)];
编译干净,可能表明我的例程名称由于某种原因被忽略。
有人有想法么?谢谢。