简体   繁体   English

如何将iboutlet插入数组(swift3)

[英]how to insert iboutlets into a array (swift3)

I am trying to group multiple iboutets into a array. 我正在尝试将多个iboutets分组为一个数组。 This will make my coding workflow much faster if its possible to group. 如果可以分组,这将使我的编码工作流程更快。 I have tried to code my idea but it does not work. 我试图编码我的想法,但是它不起作用。 What I am trying to do revolves around the let c = part. 我想做的事情围绕着让c =部分。 Code is below. 代码如下。

    import UIKit

class ViewController: UIViewController {
let c = {
@IBOutlet var a: UIButton!

@IBOutlet var b: UIButton!
}
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
}

@IBAction func turnOff(_ sender: Any) {
    c.isHidden = true

}}

Depending on your needs I see three options: 根据您的需求,我看到三个选项:

1. Outlet Collections 1.出口集合

If you use interface builder, look for the Outlet Collection option when dragging out an outlet. 如果您使用接口构建器,请在拖出插座时查找“插座收集”选项。

插座系列

Here is the syntax for manually creating outlet collections: 这是手动创建插座集合的语法:

@IBOutlet var buttons: [UIButton]!

Now you can connect multiple buttons to the collection. 现在,您可以将多个按钮连接到集合。 The downside is that you no longer have simple access to individual buttons via this outlet. 缺点是您不再可以通过此插座简单地访问各个按钮。

2. Computed Property 2.计算财产

If you would like to keep individual outlets for easy access I would probably create a computed property returning a collection: 如果您想保留各个出口以便于访问,我可能会创建一个返回集合的计算属性:

@IBOutlet var a: UIButton!
@IBOutlet var b: UIButton!
var buttons: [UIButton] {
    return [a, b]
}

3. View Hierarchy 3.查看层次结构

Depending on your layout needs you may be able to put all buttons into a common parent view. 根据您的布局需求,您可以将所有按钮置于一个公共的父视图中。 Now just hide/unhide the parent view. 现在,只需隐藏/取消隐藏父视图。

Extending Collections for Option 1 and 2 扩展选项1和2的集合

The reason why you cannot use isHidden on the buttons collection is that Arrays don't have an isHidden property. 您不能在按钮集合上使用isHidden的原因是,数组没有isHidden属性。 You will have to iterate through the array (Tom Harrington pointed this out in the comments). 您将必须遍历数组(Tom Harrington在注释中指出了这一点)。 If you have to do that a lot you can extend arrays of buttons by adding a method that forwards to the isHidden property of the array's elements. 如果您需要做很多事情,则可以通过添加转发到数组元素的isHidden属性的方法来扩展按钮数组。 Here is an example for extending the more general type Sequence constrained to UIView elements (which includes arrays of buttons): 这是一个示例,用于扩展约束到UIView元素(包括按钮数组)的更通用的Sequence类型:

extension Sequence where Iterator.Element == UIView {
    func setHidden(_ hidden: Bool) {
        for view in self {
            view.isHidden = hidden
        }
    }
}

buttons.setHidden(true)
  1. Put the buttons inside a Stack View . 将按钮放入Stack View

  2. Drag an outlet from that Stack View . 从该Stack View拖动插座。

  3. Finally you will be able to use isHidden property on that Stack View . 最后,您将能够在该Stack View上使用isHidden属性。

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

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