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.
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
Uitableviewcelldeleteconfirmationcontrol Issue
Change Width of a Uibarbuttonitem in a Uinavigationbar
Swiftui - Animations Triggered Inside a View That's in a List Doesn't Animate the List as Well
How to Draw a Circle Path with Color Gradient Stroke
How to Copy SQLite Database When Application Is Launched in iOS
How to Call Method from Viewcontroller in Gamescene
Find Attributes from Attributed String That User Typed
How to Tap on a Specific Point Using Xcode Uitests
Xcode 7 Supporting Watch Os1 and Os2
Uiwebview and Safari Comparison
Changed +Load Method Order in Xcode 7
How to Calculate Actual Font Point Size in iOS 7 (Not the Bounding Rectangle)
Found an Unexpected MACh-O Header Code: 1918975009 in Xcode 6
Dial a Phone Number with an Access Code Programmatically in iOS
How to Directly Rotate Cvimagebuffer Image in iOS 4 Without Converting to Uiimage
Do We Need to Use _Weak Self Inside Uianimationblocks in Arc