简体   繁体   中英

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. I want to use the delegate method (didSelect for MKAnnotationView) to know which button was pressed.

//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); 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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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