简体   繁体   中英

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 .

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.

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

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