如何知道在 detailCalloutAccessoryView 中按下了哪个按钮

How to know which button was pressed in a detailCalloutAccessoryView

我创建了一个带有按钮堆栈视图的 detailCalloutAccessoryView(见图),其中包含三个按钮。我想使用委托方法(MKAnnotationView 的 didSelect)来知道按下了哪个按钮。

//my code logic for creating this stack view of buttons 

//设置呼出按钮属性 func setUVButton() -> UIButton { 让 uv = UIImage(systemName: "sun.haze", withConfiguration: UIImage.SymbolConfiguration(weight: .regular))?.withTintColor(.systemRed, renderingMode: .alwaysOriginal) 让 uvbutton = UIButton(类型:.custom); uvbutton.setImage(uv, for: .normal) return uv按钮 }

func setWeatherButton() -> UIButton {
    let weather = UIImage(systemName: "thermometer.sun", withConfiguration: UIImage.SymbolConfiguration(weight: .regular))?.withTintColor(.systemYellow, renderingMode: .alwaysOriginal)
    let weabutton = UIButton(type: .custom); weabutton.setImage(weather, for: .normal)
    return weabutton
}

func setCarButton() -> UIButton {
    let car = UIImage(systemName: "car", withConfiguration: UIImage.SymbolConfiguration(weight: .regular))?.withTintColor(.systemBlue, renderingMode: .alwaysOriginal)
    let carbutton = UIButton(type: .custom); carbutton.setImage(car, for: .normal)
    return carbutton
}

func createArrayOfButtons(UIButtonUV: UIButton, UIButtonWeat: UIButton, UIButtonCar: UIButton) -> [UIView] {
    let uvButton = UIButtonUV
    let weatherButton = UIButtonWeat
    let carButton = UIButtonCar
    let buttonArray = [uvButton, weatherButton, carButton]
    return buttonArray
}

func setStackViewToDetailCallOutAccessoryView (arrangedSubviews: [UIView]) -> UIStackView {
    let stackView = UIStackView(arrangedSubviews: arrangedSubviews)
    stackView.axis = .horizontal
    stackView.distribution = .fillProportionally
    stackView.alignment = .fill
    stackView.spacing = 10
    stackView.autoresizingMask = [.flexibleLeftMargin, .flexibleRightMargin, .flexibleLeftMargin, .flexibleBottomMargin, .flexibleWidth, .flexibleHeight]
    stackView.translatesAutoresizingMaskIntoConstraints = false
    return stackView
}

如何获取按下哪个按钮的详细信息?

Image of the detailCalloutAccessoryView

您可以将目标添加到所有按钮,例如:

CarButton.addTarget(self, action:#selector(handleCarButtonPressed(sender:)), for: .touchUpInside)

func handleCarButtonPressed(sender: UIButton){
   // Handle carbutton tap here
}