这是很容易做到:
1)子类UIView
创建CustomView
,添加公共function
,在您的项目:
import UIKit
class CunstomView: UIView {
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
public func printHello() {
print("hello")
}
}
2)在你的storyboard
,拖动UIView
到您vc
,并将类设置为CunstomView
,您可以看到在我的红框中:
3)点击Show the Assistant Editor
,和Ctrl拖动view
到vc
,设置名称custom
:
4)然后在vc
的viewDidload
功能,可以调用公共函数:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var custom: CunstomView!
override func viewDidLoad() {
super.viewDidLoad()
custom.printHello()
}
}
5)结果:
您需要与相关的代码来更新你的问题,并清楚地显示在您的问题是与代码。 – rmaddy
我的第一个问题是关于接口构建器的更多信息。我没有任何代码来表明这一点。我认为第二个问题会回答第一个问题。 – lusher00