【问题标题】:Error sending void(^)(UIButton *_strong) to parameter of incompatible type SEL将 void(^)(UIButton *_strong) 发送到不兼容类型 SEL 的参数时出错
【发布时间】:2015-06-26 07:23:22
【问题描述】:

我在这里尝试用功能块替换“@selector”。但是这样做会显示一个错误:

错误:将 'void (^)(UIButton *__strong)' 发送到不兼容的参数 type 'SEL'

请指导如何直接放置功能块,而不是使用“@selector”。

[_contactPhoneButton addTarget:self
                        action:^(UIButton *sender) {
  NSString *phoneNumber = [@"tel://" stringByAppendingString:sender.titleLabel.text];
  [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
 }

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    Block 不能转换为 SEL。它们是不同的类型。

    • SEL 是一个标识函数的名称,它被发送到对象。在运行时,对象使用 SEL 查找要执行的函数
    • Block是一些你可以执行的代码,它独立于Object。

    如果您仍想使用块,请参阅此link

    【讨论】:

      【解决方案2】:

      您不能将Selector 替换为Block,因为它们是不同的类型。但是,您可以考虑使用 ReactiveCocoa 之类的东西来实现您想要的那种方法。

      Reactive Cocoa

      【讨论】:

        【解决方案3】:

        你必须添加选择器,而不是阻塞:

        [_contactPhoneButton addTarget:self
                                action: @selector(buttonTapped:)];
        

        然后

        - (void) buttonTapped: (uibutton*) sender
        {
         NSString *phoneNumber = [@"tel://" stringByAppendingString:sender.titleLabel.text];
          [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
        }
        

        【讨论】:

          【解决方案4】:

          试试这个:

           [_contactPhoneButton addTarget:[^{
                  NSString *phoneNumber = [@"tel://" stringByAppendingString:sender.titleLabel.text];
                  [[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
              } copy] action:@selector(invoke) forControlEvents:UIControlEventTouchUpInside];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多