单击按钮更新标签
Updating Label on Button Click
我的主页上有两个按钮作为右箭头和左箭头,每次单击按钮时,UILabel 都会通过数组更新,例如 let names = ["John, Smith, "Ahmed", "Jack", "Kathy"]
。我有两个 UIAction,分别是 rightArrowClicked 和 leftArrowClicked。最初标签应该显示为禁用左箭头的 John,这将由 leftArrow 的 isHidden 属性 完成,同样当值为 Kathy 时应该禁用 rightArrow。除此之外,我还需要将另一个标签 w.r.t 更新为名称值,例如 john 的值为 "cute",而 kathy 的值为 "pretty",这些值来自外部库我添加了 cocoapods
,它们是在 class 的函数中生成的。我该怎么做?
这是您需要的具有简单逻辑的代码
let names = ["John", "Smith", "Ahmed", "Jack", "Kathy"]
var currentIndex = 0
leftButton.isHidden = true
@IBAction func left(_ sender: Any) {
currentIndex --
label.text = names[currentIndex]
if currentIndex == 0 {
leftButton.isHidden = true
}
rightButton.isHidden = false
}
@IBAction func right(_ sender: Any) {
currentIndex ++
label.text = names[currentIndex]
if currentIndex == names.count - 1 {
rightButton.isHidden = true
}
leftButton.isHidden = false
}
您可以像下面这样尝试并在 viewDidLoad 方法中调用 updateButtonsState()
let names = ["John", "Smith", "Ahmed", "Jack", "Kathy"]
let nature = ["cute", "pretty", "cute", "pretty", "cute"]
var currenIndex = 0
@IBAction func leftBtnClick(_ sender: UIButton) {
currenIndex = currenIndex - 1
updateButtonsState()
}
@IBAction func rignthBtnClick(_ sender: UIButton) {
currenIndex = currenIndex + 1
updateButtonsState()
}
func updateButtonsState() {
leftBtn.isEnabled = currenIndex > 0 ? true : false
rightBtn.isEnabled = currenIndex < names.count - 1 ? true : false
if currenIndex >= 0 && currenIndex < names.count {
lblName.text = names[currenIndex]
lblNameWithNature.text = nature[currenIndex]
}
print("currenIndex == \(currenIndex)")
}
我的主页上有两个按钮作为右箭头和左箭头,每次单击按钮时,UILabel 都会通过数组更新,例如 let names = ["John, Smith, "Ahmed", "Jack", "Kathy"]
。我有两个 UIAction,分别是 rightArrowClicked 和 leftArrowClicked。最初标签应该显示为禁用左箭头的 John,这将由 leftArrow 的 isHidden 属性 完成,同样当值为 Kathy 时应该禁用 rightArrow。除此之外,我还需要将另一个标签 w.r.t 更新为名称值,例如 john 的值为 "cute",而 kathy 的值为 "pretty",这些值来自外部库我添加了 cocoapods
,它们是在 class 的函数中生成的。我该怎么做?
这是您需要的具有简单逻辑的代码
let names = ["John", "Smith", "Ahmed", "Jack", "Kathy"]
var currentIndex = 0
leftButton.isHidden = true
@IBAction func left(_ sender: Any) {
currentIndex --
label.text = names[currentIndex]
if currentIndex == 0 {
leftButton.isHidden = true
}
rightButton.isHidden = false
}
@IBAction func right(_ sender: Any) {
currentIndex ++
label.text = names[currentIndex]
if currentIndex == names.count - 1 {
rightButton.isHidden = true
}
leftButton.isHidden = false
}
您可以像下面这样尝试并在 viewDidLoad 方法中调用 updateButtonsState()
let names = ["John", "Smith", "Ahmed", "Jack", "Kathy"]
let nature = ["cute", "pretty", "cute", "pretty", "cute"]
var currenIndex = 0
@IBAction func leftBtnClick(_ sender: UIButton) {
currenIndex = currenIndex - 1
updateButtonsState()
}
@IBAction func rignthBtnClick(_ sender: UIButton) {
currenIndex = currenIndex + 1
updateButtonsState()
}
func updateButtonsState() {
leftBtn.isEnabled = currenIndex > 0 ? true : false
rightBtn.isEnabled = currenIndex < names.count - 1 ? true : false
if currenIndex >= 0 && currenIndex < names.count {
lblName.text = names[currenIndex]
lblNameWithNature.text = nature[currenIndex]
}
print("currenIndex == \(currenIndex)")
}