xcode 使用 initWithCustomView 创建的 UIBarButtonItem 不会触发操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11599797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIBarButtonItem created using initWithCustomView doesn't trigger action
提问by samwyse
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:
我正在更新一些旧代码,为了在工具栏中腾出更多空间,我将按钮从测试转换为图像。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];
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.
我的新代码是从无法在 UIBarButtonItem 上设置操作复制的,我相当确定我没有犯错,因为我的文本按钮工作正常。
showSetupView() is in my AppController.m file, and the setup screen appears and disappears as the button is pressed.
showSetupView() 在我的 AppController.m 文件中,当按下按钮时设置屏幕出现和消失。
toggleKeyboard(), OTOH, is in the same file as the loadView() routine, and currently consists of this code:
toggleKeyboard(),OTOH,与 loadView() 例程在同一个文件中,目前包含以下代码:
//- (void)toggleKeyboard {
- (IBAction)toggleKeyboard:(id)sender {
NSLog(@"Entering toggleKeyboard()...");
hiddenKeyboard = !hiddenKeyboard;
[self prepareToolbarsAndStatusbar];
}
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:
不用说,虽然我看到了按钮按下动画,但我从未看到过 NSLog 消息。还有最后一个观察,是偶然的。将 setAction 选择器更改为:
[toggleKeyboardItem setAction:@selector(noSuchRoutine:)];
compiles cleanly, possibly indicating that my routine name is being ignored for some reason.
编译干净,可能表明我的例程名称由于某种原因被忽略。
Anyone have any ideas? Thanks.
谁有想法?谢谢。
回答by samwyse
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:
我找到了答案!在按钮动作没有响应 iphone 中,据说动作和目标需要设置在 上UIButton
,而不是UIBarButtonItem
. 我不知道这是否是最新版本的 Xcode 的新功能,但我猜是因为其他问题(例如我上面提到的问题)使用了不同的技术。这是我的新代码:
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:)];
回答by BangOperator
Though its too late, but for future references, I would like to quote apple docsfor the method,
虽然为时已晚,但为了将来参考,我想引用苹果文档的方法,
- (instancetype)initWithCustomView:(UIView *)customView;
The bar button item created by this method does not call the action method of its target in response to user interactions. Instead, the bar button item expects the specified custom view to handle any user interactions and provide an appropriate response.
此方法创建的条形按钮项不会响应用户交互调用其目标的操作方法。相反,条形按钮项目期望指定的自定义视图处理任何用户交互并提供适当的响应。
回答by Natan R.
Did you try
你试过了吗
-(void)toggleKeyboard
-(void)toggleKeyboard
and
和
[toggleKeyboardItem setAction:@selector(toggleKeyboard)];
without :
[toggleKeyboardItem setAction:@selector(toggleKeyboard)];
没有 :
and it made any difference? Is the method declared in the interface file?
它有什么不同吗?方法是否在接口文件中声明?