ios - 从两个不同 View 切换多个标签

标签 ios arrays swift label

我实现了一个按钮操作,从一个带有 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/

相关文章:

ios - 获取并显示流 URL 数据

objective-c - 简单的多边形纹理贴图/iOS/cocos2d

php - 遍历 SimpleXML 对象 PHP

ios - 在 Swift 中枚举 View 的 NSLayoutConstraints?

ios - 真正理解iOS中的NSData、NSMutableData、NSString应该学什么?

iphone - 将本地 css 注入(inject) UIWebView

ios - 如何在 appcelerator 中裁剪图库图像?

c - 将评论读入动态大小的数组

ruby - 如何检查数组是否重复?

swift - @ObjectBinding 不适用于协议(protocol)