我实现了一个按钮操作,从一个带有 UIlabel.text 的 View 切换到另一个 View ,该 View 将显示另一个对应的特定标签。
我目前使用的方法如下:
func switchCard(_ sender: Any) {
if item1.text == "Fabulae"{
item2.text = " expriment"
}
if item1.text == " simulacra"{
item2.text = "finxere"
}
if item1.text == "tergentes"{
item2.text = "icet"
} }
它的工作原理,但我需要为每个项目重复一百次,所以我想知道它是否确实存在一种更简单的方法
我正在考虑使用项目 1 和项目 2 的标签构建变量,这样它们将共享相同的索引并实现单个语句,这将使我在单击按钮时能够快速获取正确的内容。
我走的路正确吗?
提前感谢您阅读我的文章以及您的善意帮助。
问候,
最佳答案
试试这个:
enum TypeName: String {
case fabulae = "Fabulae"
case simulacra = " simulacra"
case tergentes = "tergentes"
func getNeededText() -> String {
switch self {
case .fabulae: return " expriment"
case .simulacra: return "finxere"
case .tergentes: return "icet"
}
}
}
func switchCard(_ sender: Any) {
item2.text = TypeName(rawValue: item1.text ?? "")?.getNeededText()
}
或者你可以使用字典:
let pairs = ["Fabulae": " expriment", " simulacra": "finxere", "tergentes": "icet"]
func switchCard(_ sender: Any) {
item2.text = pairs[item1.text]
}
关于ios - 从两个不同 View 切换多个标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44497242/