ios - 如何从按钮集合中找到位置?

标签 ios swift

程序员们! :]

请帮帮我。我有一组按钮

@IBOutlet var buttonCollection: [UIButton]!

如何找到被按下的那个?

@IBAction func pressButtonAction(_ sender: UIButton) {
.....
    for i in 0...array.count - 1 {
        buttonCollection[i].setTitle(String(array[i]), for: .normal)
    }
.....
}

我只想将Title 设置为那些未按下的按钮。

除了标签之外我还能使用什么?

谢谢!

最佳答案

您可以将按钮引用 sender 与 Buttons 数组进行比较。

@IBAction func pressButtonAction(_ sender: UIButton) {

    for i in 0..<array.count {
        //Compare sender with array
        if sender != buttonCollection[i] {
            buttonCollection[i].setTitle(String(array[i]), for: .normal)
        }
    }
}

关于ios - 如何从按钮集合中找到位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42650479/

相关文章:

ios - 在 Swift 3.0 中检测两个对象之间的碰撞?

ios - 如何用动画重新加载 iCarousel?

ios - XCUITest - 打开 iOS 设置并更改日期/时间

ios - 将 Tabbar 插入 UITabBarController 内的 ViewController 时不会隐藏

ios - iPhone 应用程序无法启动

ios - 将条形按钮项添加到导航项的左侧

IOS7 navigatinBar tintColor popover 变化

ios - 在 IOS 12.1 上使用 WIFI 时,我的应用程序停止运行

ios - ionic 电容器 - 推送通知在 iOS 上不发出声音

ios - 如何在 Swift 中创建 FBOpenGraphAction?