Swift 多个按钮通过同一个 segue 发送字符串

Swift Multiple buttons sending String through same segue

正在寻找一些说明,只是为了仔细检查我是否走在正确的道路上。我有一个类别列表,其中多个按钮将通过 segue 发送特定的字符串。我有每个按钮的 IBOutlets,但想确保在触摸特定按钮时发送特定字符串。我只是不确定我设置 segue 的方式是否正确,以便每个按钮都特定于设置的字符串。到目前为止,当前的 segue 适用于 "attractionsButton",但是当我点击其他按钮时,它会传递相同的数据。我知道它没有设置,但想确保当另一个按钮被点击时它不会发送错误的字符串。

@IBOutlet weak var attractionsButton: UIButton!
@IBOutlet weak var eatingButton: UIButton!
@IBOutlet weak var financialButton: UIButton!
@IBOutlet weak var lodgingButton: UIButton!
@IBOutlet weak var medicalButton: UIButton!
@IBOutlet weak var publicButton: UIButton!
@IBOutlet weak var servicesButton: UIButton!
@IBOutlet weak var storesButton: UIButton!
@IBOutlet weak var transportationButton: UIButton!

let attractions = "Attractions & Entertainment"
let eating = "Eating & Drinking"
var financial = "Financial Institution"
var lodging = "Lodging Establishment"
var medical = "Medical & Health"
var publicService = "Public Services & Buildings"
var services = "Service"
var stores = "Stores & Shopping"
var transportation = "Transportation"


@IBAction func attractionsButton(_ sender: Any) {
    performSegue(withIdentifier: "category", sender: self)
}
@IBAction func eatingButton(_ sender: Any) {
    performSegue(withIdentifier: "category", sender: self)
}
@IBAction func financialButton(_ sender: Any) {
    performSegue(withIdentifier: "category", sender: self)
}
@IBAction func lodgingButton(_ sender: Any) {
    performSegue(withIdentifier: "category", sender: self)
}
@IBAction func medicalButton(_ sender: Any) {
    performSegue(withIdentifier: "category", sender: self)
}
@IBAction func publicButton(_ sender: Any) {
    performSegue(withIdentifier: "category", sender: self)
}
@IBAction func serviceButton(_ sender: Any) {
    performSegue(withIdentifier: "category", sender: self)
}
@IBAction func storesButton(_ sender: Any) {
    performSegue(withIdentifier: "category", sender: self)
}
@IBAction func transportationButton(_ sender: Any) {
    performSegue(withIdentifier: "category", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    if segue.identifier == "category" {
        if let button1 = attractionsButton {
            let user = attractions
            let controller = segue.destination as? CategoryListedViewController
            controller?.categoryList = user
        }
    }
}

在你的动作中将自己更改为发送者,你可以对所有按钮使用这个动作

@IBAction func transportationButton(_ sender: Any) {
        performSegue(withIdentifier: "category", sender: sender)
}

在准备 segue 时使用此代码

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    super.prepare(for: segue, sender: sender)
    if segue.identifier == "category" {

        let senderButton = sender as! UIButton

        switch senderButton{
        case attractionsButton:
            let user = attractions
            let controller = segue.destination as? CategoryListedViewController
            controller?.categoryList = user
        case eatingButton:
            //editing button scenario
            print("editing button scenario")
        default:
            //default code
            print("default scenario")
        }

    }
}

你所有的按钮都可以连接到这一个 @IBAction:

@IBAction func allButtons (_ sender: Any) {
    performSegue(withIdentifier: "category", sender: sender)
}

当然,如果这就是所有按钮所做的,您可以完全跳过 @IBAction 的使用,直接从按钮连接 segues。如果您在 Storyboard 中创建第一个按钮时这样做,您可以复制该按钮,所有副本都将连接到相同的 segue。

然后在 prepare(for:sender:) 中,将发件人与您的 @IBOutlet 进行比较以设置您的字符串:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let button = sender as? UIButton,
       let controller = segue.destination as? CategoryListedViewController,
       segue.identifier == "category" {

        let str: String

        switch button {
        case attractionsButton: str = attractions
        case eatingButton:      str = eating
        case financialButton:   str = financial
        default: str = ""
        }

        controller.categoryList = str
    }
}

如果我是你,我会使用按钮标签和一个很好的旧枚举来解决这个问题:

首先,在 Interface Builder 中,您设置每个按钮的 tag 属性(您甚至不再需要它们,因为 @IBOutlet

1 个用于 attractionsButton,2 个用于 eatingButton,等等

然后,您创建一个枚举,原始值为 Int,匹配值为:

enum Category : Int, CustomStringConvertible {
    case attractions = 1
    case eating = 2
    case financial = 3
    case lodging = 4
    case medical = 5
    case publicService = 6
    case services = 7
    case stores = 8
    case transportation = 9

    var description : String {
        switch self {
        case .attractions: return "Attractions & Entertainment"
        case .eating: return "Eating & Drinking"
        case .financial: return "Financial Institution"
        case .lodging: return "Lodging Establishment"
        case .medical: return "Medical & Health"
        case .publicService: return "Public Services & Buildings"
        case .services: return "Service"
        case .stores: return "Stores & Shopping"
        case .transportation: return "Transportation"
        }
    }
}

在那之后,你 link 将你所有的按钮变成只有一个 @IBAction 就像这个:

@IBAction func onButtonTap(_ sender: UIButton) {
    performSegue(withIdentifier: "category", sender: Category(rawValue: sender.tag))
}

这样,根据按钮的标签,您将创建所需的枚举。

最后,在你的 segue 准备中,你可以这样设置:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "category" {
        guard
            let controller = segue.destination as? CategoryListedViewController,
            let category = sender as? Category
            else { return }

        controller.categoryList = category.description
    }
}

这样,事情就更简洁了,您可以将更多行为附加到您的枚举 Category,在开关中使用它,等等,而不是依赖 Strings 或乘以复制和粘贴代码.