Loading Viewcontroller from Xib File

Swift - how to load a xib file from a view controller in the story board?

Solved it...it was right in front of my eyes, guess i had to ask the question to write the answer myself, haha, I'm so smart, i added this code to the view controller from the story board and it works like a charm.

import UIKit

class ViewController: UIViewController, communicationControllerM {

@IBOutlet weak var Btn: UIButton!

override func viewDidLoad() {
super.viewDidLoad()

Btn.addTarget(self, action: #selector(callMenuModal), forControlEvents: .TouchUpInside)
}

func callMenuModal() {
let mainVC = MainViewController()
mainVC.delegate = self
mainVC.modalPresentationStyle = .OverCurrentContext
presentViewController(mainVC, animated: false, completion: nil)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

func backFromM() {
self.dismissViewControllerAnimated(true, completion: nil)
}
}

in my xib view controller i added a return action

playBtn.addTarget(self, action: #selector(goBackToMainVC), forControlEvents: .TouchUpInside)
self.delegate?.backFromM()

Thanks guys!!

How i open XIb file button to ViewController

  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.

Sample Image

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

}

How to Load a viewController from a Xib file using segue? Swift 5

performSegue is one of UIViewController's methods, so it doesn't work on UICollectionViewCell. Instead, you'll need to call performSegue from the parent view controller that contains the collection view.

You can use delegates or closures for this, but I prefer closures. First, add one inside PetNameInfoCollectionViewCell:

class PetNameInfoCollectionViewCell: UICollectionViewCell {
var photoTapped: (() -> Void)? /// here!

@IBAction func takeAPhoto(_ sender: UIButton) {
photoTapped?() /// call it
}
}

Then, assign the closure inside the parent view controller's cellForItemAt.

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 0 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: userNameInfoCollectionViewCellId, for: indexPath) as! userNameInfoCollectionViewCell /// replace with the cell class
cell.photoTapped = { [weak self] in
self?.performSegue(withIdentifier: "UIImagePickerSegue", sender: nil)
}
return cell

} else {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: PetNameInfoCollectionViewCell, for: indexPath) as! PetNameInfoCollectionViewCell /// replace with the cell class
cell.photoTapped = { [weak self] in
self?.performSegue(withIdentifier: "UIImagePickerSegue", sender: nil)
}
return cell
}
}

loading ViewController(containing xib) using xib

after 4 to 5 hours now I got to the point that
I was instantiating xib viewcontroller using

let vc1 = UIViewController(nibName: "SearchViewController", bundle: nil)

so now I tried to instantiate a view controller with viewcontroller constructor

let vc1 = SearchViewController(nibName: "SearchViewController", bundle: nil)


Related Topics



Leave a reply



Submit