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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25715022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 02:16:41  来源:igfitidea点击:

How to loop through all UIButtons in my Swift view?

iosxcodeswift

提问by melwyn pawar

How would I loop through all UIButtonsin my view in Swift? I would want to set all the titles to "", but my for-loop in Swift is giving an error.

我将如何UIButtons在 Swift 中遍历所有视图?我想将所有标题设置为"",但我在 Swift 中的 for 循环出现错误。

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

回答by Woodstock

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 数组。

回答by Benno Kress

Shortened and updated for Swift 3 & 4

为 Swift 3 & 4 缩短和更新

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

回答by afterxleep

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
}

回答by Nando P.

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({
    
for view in self.viewForButtons.subviews{
        if view.isKindOfClass(UIButton)
        {
            let button : UIButton = view as! UIButton
            button.setTitle("item[i]", forState: .Normal)
        }
    }
.isKindOfClass(UIButton)}) for view in filteredSubviews { //Do something }

回答by cmario

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 randomisingmy array list and with just a for loop I printing the item at index to the button title

通过randomising我的数组列表和一个 for 循环,我将索引处的项目打印到按钮标题

let subviewButtons = self.view.subviews.filter({
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)
        }
    }
}
.isKind(of: UIButton.self)}) for button in subviewButtons { //do something }

回答by J.D. Wooder

Swift 4:

斯威夫特 4:

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

回答by Matthew Mitchell

If you have UIView's within self.viewthen you need to loop through the subviews while searching for UIButton. Using the accepted answer, I made this little function to do so:

如果您有UIView's,self.view那么您需要在搜索UIButton. 使用接受的答案,我做了这个小功能:

Swift 4 + :

斯威夫特 4 +:

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

    }

}

Usage:

用法:

myView.subviews.map {
  (##代码## as? UIButton)!.enabled = false
}

Hope this helps!

希望这可以帮助!

回答by bsod

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:

要为常见用例添加一些上下文,假设按钮位于滚动视图中,并且您想突出显示点击的按钮并取消突出显示其他按钮。在这种情况下,您会将所有按钮定向到一个操作方法:

##代码##

回答by Linh

Here's a short way in Swift if you know the subview only has buttons:

如果您知道子视图只有按钮,那么这是 Swift 中的一个简短方法:

##代码##