我如何打开 XIb 文件按钮 ViewController

How i open XIb file button to ViewController

当我点击这个 edit 按钮时,我想打开 WolverineVC

    class XibFileController: UIView {

           // This Is XIB controller File
           @IBAction func editButtonTapped(_ sender: UIButton) {
               // TODO: Click here to open wolverineVC
           }
    }

这里是 Xib 文件

这是我的故事板文件

let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(identifier: "premiumPage")
self.present(viewController, animated: true, completion: nil)
  • 首先,让我们创建一个故事板,然后命名(如果您的故事板名称是“Main.storyboard”,则名称是“Main”或Wolverine.storyboard -> 名称是:“Wolverine”)
  • 转到故事板,单击要打开的页面并为其指定标识符。 (名称为Storyboard ID,见图2)
  • 现在将以上代码添加到 editButtonTapped 函数
  • 就是这样
  guard let viewController = UIStoryboard(name: "Wolverine", bundle: nil).instantiateViewController(withIdentifier: "WolverineVC") as? WolverineVC
        else {
            return
        }
        self.navigationController?.pushViewController(viewController, animated: true)

您需要添加故事板 ID 来实例化您的故事板。

编辑:根据您更新的问题。

protocol XibDelegate: AnyObject {
 func openWolverineVC(value: Bool)
}

class XibFileController: UIView {

        weak var delegate: XibDelegate?

       // This Is XIB controller File
       @IBAction func editButtonTapped(_ sender: UIButton) {
           self.delegate?.openWolverineVC(value: true)
       }
}

你的主视图控制器应该是这样的:

    final class MainViewController: UIViewController, XibDelegate {
    

  override func viewDidLoad() {
     super.viewDidLoad() 
     xibReference.delegate = self
  }

  func openWolverineVC(value: Bool) {
   guard let viewController = UIStoryboard(name: "Wolverine", bundle: nil).instantiateViewController(withIdentifier: "WolverineVC") as? WolverineVC
            else {
                return
            }
     viewController.boolValue = value
     self.present(viewController, animated: true)
  }

}