1

I'm trying to load a VC from xib and present it on top of other VCs. It's displayed fine, but its buttons aren't working although they visually respond to taps. All connections in Interface Builder are set up correctly. When I, for example, change button title programmatically in MyVC, it's changed as expected. This is how I load my view controller from xib:

MyVC.swift:

public init() {
    super.init(nibName:String(describing: MyVC.self), bundle:bundleName)
}

required public init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

Showing it:

public func show() {
    if let rootView = UIApplication.shared.keyWindow {
        view.frame = rootView.bounds
        rootView.addSubview(view)
    }
}

In another VC somewhere in the app:

MyVC.show()

There was a similar question without an answer: Load a UIView from nib in Swift

Update:

This is the code for the button

@IBOutlet var dismissButton: UIButton!

@IBAction func dismissPopup() {
    view.removeFromSuperview()
}

IB

A breakpoint in dismissPopup() is never triggered

DP's
  • 209
  • 1
  • 4
nick130586
  • 801
  • 1
  • 9
  • 21
  • can you add code for button in myvc. – KKRocks Jun 23 '17 at 11:17
  • Try to rename your function like this: `@IBAction func dismissPopup(_ sender: AnyObject)` – BoilingFire Jun 23 '17 at 11:39
  • Thanks, I tried it, didn't work – nick130586 Jun 23 '17 at 11:46
  • 1
    Is there a specific reason you are adding the *view* from the xib as a subview of `UIApplication.shared.keyWindow` instead of showing it as a modal "popup" or as a subview of the current view? – DonMag Jun 23 '17 at 12:27
  • I think this is the right hint. I wanted to keep the logic of presenting the controller in the controller itself. Now I tried to present it using if let rootVC = UIApplication.shared.keyWindow?.rootViewController { rootVC.present(myCustomController, animated: true, completion:nil) } in another VC and it worked. I will only need to change its presentation style – nick130586 Jun 23 '17 at 13:14
  • Correction: I can still keep show() method in controller class with the code I posted above (rootVC.present) – nick130586 Jun 23 '17 at 14:59

0 Answers0