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

时间:2020-07-31 16:11:23

标签: ios swift xcode mkannotationview

我创建了一个detailCalloutAccessoryView,其中包含Buttons的堆栈视图(参见图片),其中包含三个按钮。我想使用委托方法(对于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(type:.custom); uvbutton.setImage(uv,for:.normal) 返回uvbutton }

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

1 个答案:

答案 0 :(得分:0)

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

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

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