查找视图上的所有 detailDislosureButtons
Posted
技术标签:
【中文标题】查找视图上的所有 detailDislosureButtons【英文标题】:Finding all detailDislosureButtons on a view 【发布时间】:2017-05-23 02:02:40 【问题描述】:我正在尝试在 UIViewController
上查找特定类型 (detailDisclosure
) 的所有按钮。要在视图上查找所有 UIButtons
,我会使用以下内容:
let buttons = view.subviews.filter$0 is UIButton
如何按按钮类型(在本例中为 detailDisclosure
)进行过滤?
我尝试使用原始值为 2 的 UIButtonType 和 UIButtonType.detailDisclosure
,但出现编译器错误。
let buttons = view.subviews.filter$0 is UIButtonType.detailDisclosure
感谢您的阅读。
【问题讨论】:
【参考方案1】:在您的过滤器闭包中,您首先需要使用条件类型转换运算符 (as?
) 检查每个视图是否为 UIButton
。如果是按钮,则可以检查buttonType
属性是否为.detailDisclosure
。
let buttons = view.subviews.filter
guard let button = $0 as? UIButton else
return false
return button.buttonType == .detailDisclosure
对于等效的单行解决方案,您可以使用带有 buttonType
属性的可选链接,但请注意,您必须将类型附加到 .detailDisclosure
(UIButtonType
) 上,因为它无法再被推断出来。
buttons = view.subviews.filter ($0 as? UIButton)?.buttonType == UIButtonType.detailDisclosure
【讨论】:
【参考方案2】:试试这个....
let buttons = view.subviews.filter (v) -> Bool in
if let btn = v as? UIButton
if btn.buttonType == .detailDisclosure
return true
return false
【讨论】:
【参考方案3】:这是 swift 2.3,但你可以在你的过滤方法中做这样的事情
if buton.buttonType == UIButtonType.DetailDisclosure
【讨论】:
ButtonType 是一个只读属性,所以它可能工作...您是否更改了我粘贴的代码以匹配您的?【参考方案4】:这个非常适合我:(Swift 3)
let buttons = view.subviews.filter$0 is UIButton
for btn in buttons as! [UIButton]
if btn.buttonType == .detailDisclosure
print(bin)
【讨论】:
以上是关于查找视图上的所有 detailDislosureButtons的主要内容,如果未能解决你的问题,请参考以下文章
有没有办法使用 SQL Server 2012 查找视图中引用的所有无效列?