zeeple
zeeple

Reputation: 5607

Contents of XIB not loading

In my app I have a custom UIViewController which has a corresponding XIB file. I have created a couple of buttons and labels in the XIB file and created outlets to the custom UIViewcontroller. I then push this view controller onto the navigation stack via the usual means:

let challengeDetails = ChallengeDetails()
self.navigationController!.pushViewController(challengeDetails, animated: true)

I know that the view controller is being pushed onto the stack because I set the background color of its view to red and what I get is a blank red view. What I don't get is why I do not see the labels and buttons I created in the XIB file.

I have checked the usual slew of things: File's Owner is set to my custom class. the view of the xib is connected to the view controller (in Outlets).

When I created the project I chose 'Tabbed application' and it came along with a story board. However, when I created the XIB file I did so by simply selecting the checkbox when creating a new view controller subclass. Does the XIB have to be connected to the story board for it to work?

Any help would be appreciated. If it helps, here is the custom class:

import UIKit

class ChallengeDetails: UIViewController {

    // Outlets from xib file.
    @IBOutlet weak var challengeDescriptionTextView: UITextView!
    @IBOutlet weak var challengeTitleLabel: UILabel!
    @IBAction func browseTitlesButton(sender: UIButton) {
        browseTitlesInChallenge()
    }
    @IBAction func acceptChallengeButton(sender: UIButton) {
        acceptThisChallenge()
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.redColor()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func browseTitlesInChallenge() {

    }

    func acceptThisChallenge() {

    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

Upvotes: 2

Views: 4242

Answers (3)

Jaya Mayu
Jaya Mayu

Reputation: 17247

You should load your XIB file when initialising the ViewController. You could do something like below in Swift 4.2

let detailsVC = ChallengeDetails(nibName :"DetailScreen", bundle : Bundle.main)

Please note that above DetailScreen string is the name of your XIB file.

Upvotes: 0

Aaron Rasmussen
Aaron Rasmussen

Reputation: 13316

The problem is that you're not loading the view controller from a nib, you're just initializing it with the empty initializer ChallengeDetails(). To create the view controller using the nib file that you created, use:

let challengeDetails = ChallengeDetails(nibName: "YourNibNameHere", bundle: NSBundle.mainBundle())

Make sure you set the class of your nib in Interface Builder to ChallengeDetails in the Inspector pane, too.

Upvotes: 4

Caleb
Caleb

Reputation: 5616

Here's what I usually do:

let yourXIB = NSBundle.mainBundle().loadNibNamed(
            "yourXIBName",
            owner: nil,
            options: nil)[0] as! yourXIBClass
view.addSubview(yourXIB)

I also keep all of my XIB's @IBOutlets inside of my XIB's class.

Upvotes: 3

Related Questions