【问题标题】:Warning whilst putting IBOutlet buttons in an array将 IBOutlet 按钮放入数组时发出警告
【发布时间】:2020-06-22 19:36:47
【问题描述】:

我试图将 9 个按钮放入一个数组中,但出现错误

不能在属性初始化器中使用实例成员“oneOne”;属性初始化器在“self”可用之前运行

每个按钮 9 次,这是我的代码。非常感谢任何帮助,我是 iOS 开发的新手。

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

@IBOutlet var oneOne: UIButton!

@IBOutlet var twoOne: UIButton!

@IBOutlet var threeOne: UIButton!

@IBOutlet var oneTwo: UIButton!

@IBOutlet var twoTwo: UIButton!

@IBOutlet var threeTwo: UIButton!

@IBOutlet var oneThree: UIButton!

@IBOutlet var twoThree: UIButton!

@IBOutlet var threeThree: UIButton!

var allSpaces = [oneOne,twoOne,threeOne,oneTwo,twoTwo,threeTwo,oneThree,twoThree,threeThree]

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // Get the new view controller using segue.destination.
    // Pass the selected object to the new view controller.
}
*/

谢谢

【问题讨论】:

  • 或者,使用 IB 将这些按钮添加到 Outlet Collection。
  • 行前添加lazy

标签: ios arrays swift xcode iboutlet


【解决方案1】:

allSpaces 使用lazy var 声明或创建@IBOutlet 集合:

lazy var allSpaces = [oneOne,twoOne,threeOne,oneTwo,twoTwo,threeTwo,oneThree,twoThree,threeThree]

或:

@IBOutlet weak var buttons: [UIButton]! // Connect your outlets here.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-08
    • 2020-02-02
    相关资源
    最近更新 更多