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.
有人有主意吗?谢谢。
3 个解决方案
#1
16
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:)];
#2
0
Did you try
你试过了吗
-(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?
它有什么区别?方法是在接口文件中声明的吗?
#3
0
Though its too late, but for future references, I would like to quote apple docs for 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.
此方法创建的条形按钮项不会响应用户交互调用其目标的操作方法。相反,bar按钮项期望指定的自定义视图处理任何用户交互并提供适当的响应。
#1
16
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:)];
#2
0
Did you try
你试过了吗
-(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?
它有什么区别?方法是在接口文件中声明的吗?
#3
0
Though its too late, but for future references, I would like to quote apple docs for 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.
此方法创建的条形按钮项不会响应用户交互调用其目标的操作方法。相反,bar按钮项期望指定的自定义视图处理任何用户交互并提供适当的响应。