Search code examples
swiftxcodestoryboardxib

How i open XIb file button to ViewController


When i tap this edit button then i want to open WolverineVC

    class XibFileController: UIView {

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

here is the Xib Fileenter image description here

here is my Storyboard file

enter image description here


Solution

  •   guard let viewController = UIStoryboard(name: "Wolverine", bundle: nil).instantiateViewController(withIdentifier: "WolverineVC") as? WolverineVC
            else {
                return
            }
            self.navigationController?.pushViewController(viewController, animated: true)
    

    You need to add a storyboard ID to instantiate your storyboard.

    enter image description here

    edit: according to your updated question.

    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)
           }
    }
    

    Your main view controller should be like this:

        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)
      }
    
    }