【问题标题】:UIBarButtonItem created using initWithCustomView doesn't trigger action使用 initWithCustomView 创建的 UIBarButtonItem 不会触发操作
【发布时间】:2018-08-08 16:09:12
【问题描述】:

我正在更新一些旧代码,为了在工具栏中腾出更多空间,我将按钮从测试转换为图像。 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:)];

编译干净,可能表明我的例程名称由于某种原因被忽略了。

有人有什么想法吗?谢谢。

【问题讨论】:

    标签: iphone ios xcode uibarbuttonitem


    【解决方案1】:

    我找到了答案!在button action not responding 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:)];
    

    【讨论】:

    • 好答案!真正解决问题。这是将一个按钮放入另一个按钮中。
    • 是的!如此简单,完美!
    • 来自文档:“此方法创建的条形按钮项不会调用其目标的操作方法来响应用户交互。相反,条形按钮项期望指定的自定义视图来处理任何用户交互并提供适当的响应。” . UIBarButtonItem 不是 UIView 的后代。
    【解决方案2】:

    虽然为时已晚,但为了以后的参考,我想引用apple docs作为方法,

    - (instancetype)initWithCustomView:(UIView *)customView;
    

    此方法创建的条形按钮项不调用 其目标响应用户交互的操作方法。 相反,栏按钮项期望指定的自定义视图 处理任何用户交互并提供适当的响应。

    【讨论】:

      【解决方案3】:

      你试过了吗

      -(void)toggleKeyboard

      [toggleKeyboardItem setAction:@selector(toggleKeyboard)]; 没有:

      这有什么不同吗?接口文件中是否声明了方法?

      【讨论】:

      • 我已经开始这样做了,jet 证实它仍然无法正常工作。不过谢谢。那么,冒号到底是什么意思呢?
      • 当您有一个方法 -(void)myMethod; 时,您可以通过 [self myMethod] 或通过选择器 '@selector(myMethod)' 调用它。如果您有任何参数(任何类型),那么我们有-(void)myMethod:(id)myParameter;,并且在调用时,您执行[self myMethod:parameterToSend] 或通过选择器'@selector(myMethod:)',在这种情况下,UIBarButtonItem 负责发送发件人”。当您通过代码执行此操作时,我想您实际上并不需要发件人。它应该可以工作,也许您可​​以显示更多代码。并尝试将其放入 viewDidLoad 而不是 loadView。
      猜你喜欢
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-02
      相关资源
      最近更新 更多