相关疑难解决方法(0)

使用initWithCustomView创建的UIBarButtonItem不会触发操作

我正在更新一些旧代码,并在工具栏中腾出更多空间,我正在将按钮从测试转换为图像.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];
Run Code Online (Sandbox Code Playgroud)

我的新代码是从无法在UIBarButtonItem上设置动作复制的,而且我很确定我没有犯错,因为我的文本按钮工作得很好.

showSetupView()在我的AppController.m文件中,当按下按钮时,设置屏幕出现并消失.

toggleKeyboard(),OTOH,与loadView()例程在同一个文件中,目前包含以下代码:

//- (void)toggleKeyboard {
- (IBAction)toggleKeyboard:(id)sender {
    NSLog(@"Entering toggleKeyboard()...");
    hiddenKeyboard = !hiddenKeyboard;
    [self prepareToolbarsAndStatusbar];
}
Run Code Online (Sandbox Code Playgroud)

不用说,虽然我看到按钮动画,但我从未看到过NSLog消息.最后一次观察是偶然发现的.将setAction选择器更改为:

[toggleKeyboardItem setAction:@selector(noSuchRoutine:)];
Run Code Online (Sandbox Code Playgroud)

干净地编译,可能表示我的例行名称由于某种原因被忽略.

有人有主意吗?谢谢.

iphone xcode uibarbuttonitem ios

8
推荐指数
1
解决办法
8917
查看次数

标签 统计

ios ×1

iphone ×1

uibarbuttonitem ×1

xcode ×1