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

时间:2018-04-05 02:16:56

标签: ios swift uistoryboardsegue

寻找一些澄清,并仔细检查我是否在正确的道路上。我有一个类别列表,其中多个按钮将通过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
        }
    }
}

3 个答案:

答案 0 :(得分:1)

将您的操作中的自我更改为发件人,您可以对所有按钮使用此操作

SELECT s1.state, s1.Estimates_Base, s1.2010, s1.2011, s1.2012
FROM State_1 AS s1
LEFT JOIN State_2 AS s2 ON s1.state_id = s2.state_id;

在准备segue

时使用此代码
@IBAction func transportationButton(_ sender: Any) {
        performSegue(withIdentifier: "category", sender: sender)
}

答案 1 :(得分:1)

您的所有按钮都可以连接到@IBAction

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

当然,如果这就是所有按钮正在进行的操作,您可以完全跳过使用@IBAction并直接从按钮连接segue。如果您在故事板中创建第一个按钮时这样做,您可以复制该按钮,所有副本将连接到同一个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
    }
}

答案 2 :(得分:0)

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

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

1为景点按钮,2为吃吃按钮等。

然后,您创建一个枚举,其原始值为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"
        }
    }
}

之后,您将所有按钮链接到只有一个@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或乘以复制和粘贴代码