2

I have been following this tutorial in order to load a xib at the push of a button in a view controller: http://www.thomashanning.com/loading-a-view-from-a-xib/

At this line:

if let customView = NSBundle.mainBundle().loadNibNamed("CustomView", owner: self, options: nil).first as? CustomView {

I get the below error: Use of undeclared type 'CustomView'

I have followed the tutorial every step several times and I do not know what I am missing. Can anybody help with an idea of what could be wrong?

8
  • Tha would be obvious yes. But I did... Commented May 30, 2016 at 9:49
  • Select your CustomView.swift file and check in Xcode's information panel that its target box is correctly checked, maybe it doesn't target properly. Commented May 30, 2016 at 9:51
  • at target membership it's my project name and it is selected Commented May 30, 2016 at 9:55
  • And the file where you write if let customView ... and get this error also has the same target? Commented May 30, 2016 at 9:56
  • checked and yes, it has the same target. any more ideas? happy to try anything Commented May 30, 2016 at 9:57

3 Answers 3

2

The error indicate that you missed a step declaring the class

Use of undeclared type 'CustomView'

One of few things could be missed:

  1. You didn't create the CustomView.swift file, the tutorial skipped this part. That file should contine the following:

    import UIKit
    
    class CustomView: UIView {
      @IBAction func ButtonDidPressed(sender: AnyObject) {
        print("Button Pressed")
      }
    }
    

    Corresponding error (in editor):

    Use of undeclared type 'CustomView'

  2. In Storyboard Identity Inspector you didn't set the class name correctly.

    Corresponding error (in run time):

    Unknown class CustomView in Interface Builder file.

  3. Your class declaration is not identical to the call, but I think you should be able to identify this situation in Xcode editor.

    Corresponding error (in editor):

    Use of undeclared type 'CustomView'

Sign up to request clarification or add additional context in comments.

3 Comments

Great analysis , in first time I was thinking to set File's Owner to CustomView
Thank you for your detailed answer. I have went through all you points and none of them helped out yet. There seems to be a problem with any new class created. I can not call an instance of it, only instances of old classes I created.
Here is the working project. Try and compare. I didn't tried and fix the constrains, but otherwise followed the tutorial. link.dl.dropboxusercontent.com/u/15024098/example.zip
0

You can use this way give same name for UIView class and View/.Xib also

You can use this way give same name for UIView class and View/.Xib also

let alert = NSBundle.mainBundle().loadNibNamed("PromotionMenu", owner: nil, options: nil)[0] as? PromotionMenu
        alert?.delegate = self
        self.view.addSubview(alert!)

Comments

0

Thank you all for your detailed answers, unfortunately none of them were of help.

There was not anything logic in what was happening, as I was not able to create any new classes of any type and call them without error.

So after many hours of frustration I created a new project, dragged all my old files in it(except plist) and after a run I was able to create new classes and create instances of them without errors.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.