0

I am trying to make a popup view similar to this one. What I have done so far is:

  • Open a new single view project.
  • Added a button to the main view.
  • Added a new xib file named "popup.xib"
  • Added a new swift file named "PopupViewController.swift"
  • At the identity tab I made the first responders class to be "PopupViewController"
  • I put in the popup.xib a label, a button and a view with different background colour. Of course everything has constraints about where it should appear.

My code:

ViewController.swift

import UIKit

class ViewController: UIViewController {

    @IBAction func showPopup(sender: AnyObject) {
        var x = PopupViewController()
        x.show(self.view)
        x.showAnimate()
    }
}

PopupViewController

import UIKit

class PopupViewController : UIViewController {

    func show(tView : UIView) {
        tView.addSubview(self.view)
        println("here")
        self.view.backgroundColor = UIColor.redColor()

    }

    func showAnimate() {
        self.view.transform = CGAffineTransformMakeScale(1.0, 1.0)
        self.view.alpha = 0.3
    }

}

The result: When the button is pressed I get a redish overlay on the view (because the view I added is red and has 30% opacity), but the new view is empty. No button, no label, no area with different colour.

What do I have to do to make the popup.xib show it's elements?


Update

I was missing the connection between the File's Owner and the main view in addition to Nerkyators answer. Just right click the "File's Onwer" and from view drag to the main view that is two lines below it.

Ramzi Khahil
  • 4,932
  • 4
  • 35
  • 69

1 Answers1

0

You need to load manually the xib associated to your view. I use this extensions (found at this link) modified to support UIViewController and then I call it when needed.

extension UIViewController {
    class func loadFromNibNamed(nibNamed: String, bundle : NSBundle? = nil) -> Self? {
        return UINib(
            nibName: nibNamed,
            bundle: bundle
            ).instantiateWithOwner(nil, options: nil)[0] as? UIViewController
    }
}

then

@IBAction func showPopup(sender: AnyObject) {
    var x = PopupViewController.loadFromNibNamed("popup")
    //do what you need with x
}
Community
  • 1
  • 1
Nerkyator
  • 3,976
  • 1
  • 25
  • 31
  • Thanks for the answer. For some reason I get `'PopupViewController.Type' does not have a member named 'loadControllerFromNibNamed'`. Tried to put it in both files and in a separate file. Nothing worked. (I tried to make clean an build again) – Ramzi Khahil Feb 02 '15 at 10:36
  • I edited my previous reply, there was a typo on it. Now it is correct (no more "Controller" in `loadFromNibNamed`). – Nerkyator Feb 02 '15 at 10:38
  • Now it compiles, but for some reason x is nil, and I get an error when I access it. – Ramzi Khahil Feb 02 '15 at 10:45
  • The "popup" which is sent as an argument to `loadFromNibNamed`, should be the file name, right? – Ramzi Khahil Feb 02 '15 at 10:47
  • exactly, you have to use the xib filename – Nerkyator Feb 02 '15 at 10:55
  • I do (even copy-pasted the name). It returns me nil. :-\ – Ramzi Khahil Feb 02 '15 at 10:59
  • Try to load it manually without extensions: var x = PopupViewController(nibName: "popup", bundle: nil) [manual page](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIViewController_Class/index.html#//apple_ref/occ/instm/UIViewController/initWithNibName:bundle) – Nerkyator Feb 02 '15 at 11:09
  • Perfect, glad to hear that! – Nerkyator Feb 02 '15 at 11:21
  • There is a [follow up question](http://stackoverflow.com/questions/28278345/does-not-have-a-member-instantiatewithowner), if you are interested. – Ramzi Khahil Feb 02 '15 at 13:02
  • 1
    By using `Self?` as the return type, you can avoid casting the result. – David Berry Feb 02 '15 at 17:20