使用initWithCustomView创建的UIBarButtonItem不会触发操作 [英] UIBarButtonItem created using initWithCustomView doesn't trigger action
问题描述
我正在更新一些旧代码,并在工具栏中腾出更多空间,我正在将按钮从测试转换为图像。 loadView中新旧代码的一个示例如下:
I'm updating some old code, and to make more room in a toolbar, I'm converting the Buttons from test to images. An example of the new and old code in loadView is this:
// 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];
我的新代码是从无法在UIBarButtonItem上设置动作,而且我很确定我没有犯错,因为我的文本按钮工作正常。
My new code is copied from Cannot set action on UIBarButtonItem, and I'm fairly certain that I'm not making their mistake since my text button is working just fine.
showSetupView()在我的AppController.m文件中,当按下按钮时,设置屏幕出现并消失。
showSetupView() is in my AppController.m file, and the setup screen appears and disappears as the button is pressed.
toggleKeyboard(),OTOH,与loadView()例程在同一个文件中,目前包含以下代码:
toggleKeyboard(), OTOH, is in the same file as the loadView() routine, and currently consists of this code:
//- (void)toggleKeyboard {
- (IBAction)toggleKeyboard:(id)sender {
NSLog(@"Entering toggleKeyboard()...");
hiddenKeyboard = !hiddenKeyboard;
[self prepareToolbarsAndStatusbar];
}
毋庸置疑,虽然我看到按钮动画,但我从未见过NSLog消息。最后一次观察是偶然发现的。将setAction选择器更改为:
Needless to say, although I see the button-press animation, I never see the NSLog message. And one last observation, made by accident. Changing the setAction selector to this:
[toggleKeyboardItem setAction:@selector(noSuchRoutine:)];
干净利落地编译,可能表示我的例行名称因某种原因被忽略。
compiles cleanly, possibly indicating that my routine name is being ignored for some reason.
任何人都有任何想法?谢谢。
Anyone have any ideas? Thanks.
推荐答案
我找到了答案!在按钮操作无响应iphone 中,表示需要设置操作和目标在 UIButton
上,而不是 UIBarButtonItem
。我不知道Xcode的最新版本是否是新的,但我想是因为其他问题(例如我上面提到的问题)使用了不同的技术。这是我的新代码:
I found the answer! In button action not responding iphone, it's said that the action and target need to be set on the UIButton
, not the UIBarButtonItem
. I don't know if that's new with the latest version of Xcode, but I guess it is since other questions (such as the one I mention above) use a different technique. Here's my new code:
UIButton *toggleKeyboardButton = [UIButton buttonWithType:UIButtonTypeCustom];
toggleKeyboardButton.bounds = CGRectMake( 0, 0, keyboardAddImage.size.width, keyboardAddImage.size.height );
[toggleKeyboardButton setImage:keyboardAddImage forState:UIControlStateNormal];
[toggleKeyboardButton addTarget:self action:@selector(toggleKeyboard) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *toggleKeyboardItem = [[UIBarButtonItem alloc] initWithCustomView:toggleKeyboardButton];
//[toggleKeyboardItem setTarget:self];
//[toggleKeyboardItem setAction:@selector(toggleKeyboard:)];
这篇关于使用initWithCustomView创建的UIBarButtonItem不会触发操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!