简体   繁体   English

从两个不同的角度切换多个标签

[英]switch of multiple labels from two different views

I implemented a button action that switch from one view with a UIlabel.text to another view that will display another specifical label that is corresponding . 我实现了一个按钮动作,该动作从一个带有UIlabel.text的视图切换到另一个视图,该视图将显示对应的另一个特定标签。

The method i am using, at present is as follow: 我目前使用的方法如下:

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"  
    }  }  

And its working , but I need to repeat so many times a hundred time for each item so i am wondering if it does exist a simpler way 及其工作,但是我需要为每个项目重复一百次,所以我想知道它是否确实存在更简单的方法

I was thinking to build to variables with my labels for item 1 and Item2 so they will share the same index and implement a single statement that will make swift able to pick up the right content when i am cliking on the button. 我当时在考虑使用带有标签的商品1和商品2来构建变量,以便它们共享相同的索引并实现一条语句,这样在我按下按钮时就可以迅速选择正确的内容。

I am going in the correct way ? 我要走正确的路吗?

Thank you in advance to read me and your kind help . 预先感谢您阅读我和您的帮助。

Regards, 问候,

Try this: 尝试这个:

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()
}

OR you can use a dictionary: 或者您可以使用字典:

let pairs = ["Fabulae": " expriment", " simulacra": "finxere", "tergentes": "icet"]

func switchCard(_ sender: Any) {
    item2.text = pairs[item1.text]
}

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

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