简体   繁体   English

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

[英]How to know which button was pressed in a detailCalloutAccessoryView

I have created a detailCalloutAccessoryView with a stack view of Buttons (see image) there are three buttons.我创建了一个带有按钮堆栈视图的 detailCalloutAccessoryView(见图),共有三个按钮。 I want to use the delegate method (didSelect for MKAnnotationView) to know which button was pressed.我想使用委托方法(MKAnnotationView 的didSelect)来知道按下了哪个按钮。

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

//setting call out button properties func setUVButton() -> UIButton { let uv = UIImage(systemName: "sun.haze", withConfiguration: UIImage.SymbolConfiguration(weight: .regular))?.withTintColor(.systemRed, renderingMode: .alwaysOriginal) let uvbutton = UIButton(type: .custom); //设置调用按钮属性 func setUVButton() -> UIButton { let uv = UIImage(systemName: "sun.haze", withConfiguration: UIImage.SymbolConfiguration(weight: .regular))?.withTintColor(.system.Red, rendering:Mode alwaysOriginal) let uvbutton = UIButton(type: .custom); uvbutton.setImage(uv, for: .normal) return uvbutton } uvbutton.setImage(uv, for: .normal) return 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
}

how to get the details of which button pressed?如何获取按下哪个按钮的详细信息?

Image of the detailCalloutAccessoryView详细信息CalloutAccessoryView 的图像

You can add target to all buttons like:您可以将目标添加到所有按钮,例如:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM