Cannot interact IBOutlet on CustomView Xib Files

48 Views Asked by At

I'm a Swift beginner, and I'm stacking on using CustomView Xib Files on Swift.

I have five CustomViews, named HogeAView~HogeEView respectively.

From HogeAView to HogeCView, the view can loaded without describing any errors, and enabled to interact all outlets which I placed.

But HogeDView and HogeEView, that made same way as other three views are not working. Both Views are loaded, but all outlets which I placed returns no responses when I interacted. (e.g. UIButton doesn't make a flash when the button was pressed)

I confirmed Files'owner UIViewClass is correct, same name as written in Code, and there's no absurd Views which prevents interacting.

Please give me a solution if you know how to solve the problem.

Thank you.

import UIKit

class HogeDView: UIView {
 
    override init(frame: CGRect){
        super.init(frame: frame)
        loadNib()
    }

    required init(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)!
        loadNib()
    }

    func loadNib(){
        let view = Bundle.main.loadNibNamed("HogeDView", owner: self, options: nil)?.first as! UIView
        view.frame = self.bounds
        self.addSubview(view)
    }
    
}
import UIKit

class HogeDViewController: UIViewController {
    
    override func loadView() {
        super.loadView()
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view = HogeDView()
    }
}

enter image description here

  • Filled HogeDView class in the **File's owner **, not in the view of the hierarchy in Xid Files.
  • Retried making HogeDview, and HogeDController.
  • Confirmed there is no absurd view upper front.
  • Confirmed UIConnection is Correct
1

There are 1 best solutions below

0
simd-float4x4 On

My first code was a copy of online tutorial...and it should had to be worked correctly...basically the tutorial I found was totally wrong.

DonMag's comments helped me to solve the issue.

// Instantiate the HogeDView from its Xib file
let hogeDView = Bundle.main.loadNibNamed("hogeDView", owner: self, options: nil)?.first as! UIView

// Set the frame for the HogeDView
hogeDView.frame = self.view.bounds
        
// Add the HogeDView as a subview to the parent view
self.view.addSubview(hogeDView)

To Donmag, and HangarRash, thank you for giving me a feedback.