快速错误"Only instance properties can be declared @IBOutlet"



我正在尝试制作圆角按钮。到目前为止,这就是我所拥有的。我一直得到";只有实例属性可以声明为@IBOutlet";错误

import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

}

@IBOutlet var Button: UIButton! {
didSet {
Button.backgroundColor = .clear
Button.layer.cornerRadius = 5
Button.layer.borderWidth = 0.8
Button.layer.borderColor = UIColor.black.cgColor
}

}

@IBOutlet weak var sampleButton: UIButton! {
didSet {
sampleButton.layer.cornerRadius = 5
sampleButton.layer.borderWidth = 0.8

}
}

您的ButtonsampleButton不是实例变量,因为它们是在任何类型范围之外定义的:

import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

} // <---- end of ViewController class scope

@IBOutlet var Button: UIButton! {
didSet {
Button.backgroundColor = .clear
Button.layer.cornerRadius = 5
Button.layer.borderWidth = 0.8
Button.layer.borderColor = UIColor.black.cgColor
}

}

@IBOutlet weak var sampleButton: UIButton! {
didSet {
sampleButton.layer.cornerRadius = 5
sampleButton.layer.borderWidth = 0.8

}
}

因此,您将@IBOutlet属性应用于全局变量,因此出现错误。我怀疑这不是故意的,ButtonsampleButton应该是ViewController类的实例变量。

向下移动右括号,将ButtonsampleButton包含在ViewController:中

import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

@IBOutlet var Button: UIButton! {
didSet {
Button.backgroundColor = .clear
Button.layer.cornerRadius = 5
Button.layer.borderWidth = 0.8
Button.layer.borderColor = UIColor.black.cgColor
}

}

@IBOutlet weak var sampleButton: UIButton! {
didSet {
sampleButton.layer.cornerRadius = 5
sampleButton.layer.borderWidth = 0.8

}
}
} // <---- moved the bracket down

相关内容

最新更新