我想在 View Controller 中创建一个链接到 xib 文件屏幕的按钮。我需要在 ViewController.swift 文件的 IBAction 中编写哪些代码行?
我尝试编写用于将两个 Storyboard文件连接在一起的代码,但它似乎不起作用。
这是我用来连接到 Storyboard 文件的代码示例,但我对 xib 文件使用什么代码:
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let progressVC = storyBoard.instantiateViewController(withIdentifier: "ProgressVC") as! WGProgressVC.progressVC.modalTransitionStyle = .crossDissolve
self.present(progressVC, animated: true, completion: nil)
我想将我的 Main.storyboard 文件连接到 xib 文件,但它不起作用,因为我不知道需要编写的代码行。我该如何解决这个问题?
最佳答案
您可能需要这样的东西:
@IBDesignable class MyBtn: UIButton {
// any outlets here
// @IBOutlet ...
override init(frame: CGRect) {
super.init(frame: frame)
initNib()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
initNib()
}
func initNib() {
let bundle = Bundle(for: MyBtn.self)
bundle.loadNibNamed("MyBtnXib", owner: self, options: nil)
// any customization below
...
}
}
一般来说,如果您使用 IB,最好对 UI 元素使用 IBDesignable
,对合理的变量使用 IBInspectable
。
关于swift - 如何将 ViewController.swift 中的按钮链接到 xib 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54664545/