我有UIButton的自定义按钮子类

/// A Button object with pop ups buttons
open class CircleMenu: UIButton {
  /// The object that acts as the delegate of the circle menu.
  @IBOutlet weak open var delegate: /*AnyObject?*/ CircleMenuDelegate?

......................


在同一类别的最下层,有一个检测何时选择按钮的功能。

func buttonHandler(_ sender: CircleMenuButton) {
    guard let platform = self.platform else { return }
    self.delegate?.circleMenu?(self, buttonWillSelected: sender, atIndex: sender.tag)
    let circle = CircleMenuLoader(radius: CGFloat(distance),
                                  strokeWidth: bounds.size.height,
                                  platform: platform,
                                  color: sender.backgroundColor)

    if let container = sender.container { // rotation animation
      sender.rotationAnimation(container.angleZ + 360, duration: duration)
      container.superview?.bringSubview(toFront: container)
    }

    if let buttons = buttons {
      circle.fillAnimation(duration, startAngle: -90 + Float(360 / buttons.count) * Float(sender.tag)) { [weak self] _ in
        self?.buttons?.forEach { $0.alpha = 0 }
      }
      circle.hideAnimation(0.5, delay: duration) { [weak self] _ in
        if self?.platform?.superview != nil { self?.platform?.removeFromSuperview() }
      }

      hideCenterButton(duration: 0.3)
      showCenterButton(duration: 0.525, delay: duration)

      if customNormalIconView != nil && customSelectedIconView != nil {

        DispatchQueue.main.asyncAfter(deadline: .now() + duration, execute: {
          self.delegate?.circleMenu?(self, buttonDidSelected: sender, atIndex: sender.tag)

        })
      }
    }
  }


与上面的委托变量一起,我还有另一个类来管理委托。

@objc public protocol CircleMenuDelegate {
    @objc optional func circleMenu(_ circleMenu: CircleMenu, buttonWillSelected sender: UIButton, atIndex: Int)
}


我正在尝试访问数据,尤其是访问另一类中正在按下的按钮的索引,并且正好在我正在玩的时候(如果将其设置在按钮内)

class AppMainViewController: UIViewController, CircleMenuDelegate  {

    @IBAction func getValue(_ sender: Any) {
        CircleMenuDelegate.circleMenu(CircleMenu, buttonWillSelected: UIButton, atIndex: Int)
    }
}


这引发了一个错误“无法将类型'CircleMenu'的值转换为预期的参数类型'CircleMenu'”

- - - 编辑 - - -

只是在AppMainViewController类内的函数中添加circleMenu调用即可停止上述类型错误,但是我不知道如何获取buttonWillSelected和atIndex的值。

func circleMenu(_ cm: CircleMenu, buttonWillSelected sender: UIButton, atIndex: Int) {
    print(sender.tag)
}


还有一些教科书,在线搜索显示类似以下内容:

CircleMenu().delegate = self


但是,CircleMenu()之后没有选择委托的选项。dropdown options

最佳答案

我怀疑您是在委托方法调用中而不是CircleMenu类的对象中传递类名称CircleMenu

CircleMenuDelegate.circleMenu(**Circle Menu Object Here**, buttonWillSelected: UIButton, atIndex: Int)

10-06 00:37