Ethan Brown
Ethan Brown

Reputation: 27282

UIView created in Interface Builder not rendering as expected

I'm trying to get my head around iOS development (Swift + Xcode 6), and I'm trying to figure out how what I do in the Interface Builder relates to my code.

What I've done is created PageViewController : UIPageViewController which, in its viewDidLoad method, calls setViewControllers to add a single view controller, an instance of Page11ViewController : UIViewController.

Thanks to a helpful answer on a recent question, I now know that PageViewController and Page1ViewController are being created successfully. However, I am trying to design Page1ViewController in IB, and what's being rendered doesn't reflect the work I'm doing in IB. In my storyboard, I have the following:

enter image description here

So "Page 1 View Controller Scene" contains a view that contains a label ("Page 1"). I have made "Page 1 View Controller" an instance of Page1ViewController by setting its class in the Identity Inspector for that view controller:

enter image description here

But when I run the app, I don't see my white view with a "Page 1" label; instead I see a blank red view. Why red? Because I did this:

class Page1ViewController : UIViewController {
    @IBOutlet var mainView: UIView!
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.redColor()
        println("Page 1 loaded")
        println("Subviews: \(view.subviews.count)")
    }
}

So I know that code is being executed, and the background is being set there...and the subview count printed out is 0...so I know that the view I'm constructing in my IB Storyboard is not the view that's actually getting drawn.

Here is the code from PageViewController that adds Page1ViewController:

class PageViewController : UIPageViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        println("Page view controller loaded")
        let page1vc = Page1ViewController()
        setViewControllers([page1vc],
            direction: UIPageViewControllerNavigationDirection.Forward,
            animated: true, completion: nil)
    }
}

How do I attach the view I'm creating in IB to the code?

Upvotes: 0

Views: 410

Answers (1)

Paulw11
Paulw11

Reputation: 114846

Your problem is that you are instantiating an empty Page1ViewController, rather than loading it from your storyboard so it isn't connected to any of the objects you defined in the scene.

You should use -

class PageViewController : UIPageViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        println("Page view controller loaded")
        let storyboard = UIStoryboard(name:"MainStoryboard" bundle:nil);

        let page1vc =  storyboard.instantiateViewControllerWithIdentifier("page1ViewController") as Page1ViewController // Check the Storyboard ID for your scene in the storyboard  
        setViewControllers([page1vc],
            direction: UIPageViewControllerNavigationDirection.Forward, animated: true, completion: nil)
    }
}

Upvotes: 1

Related Questions