简体   繁体   English

如何循环遍历我的 Swift 视图中的所有 UIButton?

[英]How to loop through all UIButtons in my Swift view?

How would I loop through all UIButtons in my view in Swift?我将如何在 Swift 中遍历视图中的所有UIButtons I would want to set all the titles to "" , but my for-loop in Swift is giving an error.我想将所有标题设置为"" ,但我在 Swift 中的 for 循环出现错误。

for btns in self.view as [UIButton] {
  // set the title to ""
}

This code should work:此代码应该工作:

for view in self.view.subviews as [UIView] {
    if let btn = view as? UIButton {
        btn.setTitleForAllStates("")
    }
}

You need to iterate through the subViews array.您需要遍历 subViews 数组。

Shortened and updated for Swift 3 & 4为 Swift 3 & 4 缩短和更新

for case let button as UIButton in self.view.subviews {
    button.setTitleForAllStates("")
}

Looping over subview works, but it's sometimes a little ugly, and has other issues.循环子视图有效,但有时有点难看,并且还有其他问题。

If you need to loop over some specific buttons, for example to add corner radius or change tint or background color, you can use an array of IBOutlets and then loop over that.如果您需要循环播放某些特定按钮,例如添加圆角半径或更改色调或背景颜色,您可以使用一组 IBOutlets,然后循环播放。

var buttons = [SkipBtn, AllowBtn]
for button in buttons as! [UIButton] {
    button.layer.cornerRadius = 5
}

To add some context for a common use case, suppose the buttons were in a scroll view and you wanted to highlight the tapped button and de-highlight the other buttons.要为常见用例添加一些上下文,假设按钮位于滚动视图中,并且您想突出显示点击的按钮并取消突出显示其他按钮。 In this situation, you would direct all buttons to one action method:在这种情况下,您会将所有按钮定向到一个操作方法:

@objc private func buttonAction(_ button: UIButton) {
    for case let b as UIButton in view.scrollView.subviews {
        if b == button {
            b.setTitleColor(UIColor.green, for: []) // highlight
        } else {
            b.setTitleColor(UIColor.black, for: []) // de-highlight
        }
    }
}

This code seems to be quite useful for iterating over any object within a view, just change UIButton for any other subview type such as UIView or UIImageView, etc.这段代码对于迭代视图中的任何对象似乎非常有用,只需将 UIButton 更改为任何其他子视图类型,例如 UIView 或 UIImageView 等。

let filteredSubviews = self.view.subviews.filter({
    $0.isKindOfClass(UIButton)})

for view in filteredSubviews {
    //Do something        
}

Used some of the offered questions out there and created my own.使用了一些提供的问题并创建了我自己的问题。 I believe is the most efficient when you want to programmatically set up the title of various UIButtons (in my case I am building a quiz)当您想以编程方式设置各种UIButtons的标题时,我相信这是最有效的(在我的情况下,我正在构建一个测验)

By randomising my array list and with just a for loop I printing the item at index to the button title通过randomising我的数组列表并仅使用 for 循环,我将索引处的项目打印到按钮标题

for view in self.viewForButtons.subviews{
        if view.isKindOfClass(UIButton)
        {
            let button : UIButton = view as! UIButton
            button.setTitle("item[i]", forState: .Normal)
        }
    }

Swift 4:斯威夫特 4:

let subviewButtons = self.view.subviews.filter({$0.isKind(of: UIButton.self)})

for button in subviewButtons {
    //do something        
}

If you have UIView 's within self.view then you need to loop through the subviews while searching for UIButton .如果您在self.viewUIView那么您需要在搜索UIButton时循环遍历子视图。 Using the accepted answer, I made this little function to do so:使用接受的答案,我做了这个小功能:

Swift 4 + :斯威夫特 4 +:

func findButton(`in` view: UIView){
    for view in view.subviews as [UIView] {
        if let button = view as? UIButton {
            // Do something with 'button'
        }else{
            // Loop through subview looking for buttons
            findButton(in: view)
        }
    }
}

Usage:用法:

override func viewDidLoad() {
    findButton(in: self.view)
}

Hope this helps!希望这可以帮助!

Here's a short way in Swift if you know the subview only has buttons:如果您知道子视图只有按钮,那么这是 Swift 中的一个简短方法:

myView.subviews.map {
  ($0 as? UIButton)!.enabled = false
}

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

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