Loaded nib but the 'view' outlet was not set
Asked Answered
A

33

819

I added a new nib file to my project, and tried to load it.

However, when I click on the toolbar icon that is supposed to take me to the view that I created, I get an NSInternalInconsistencyException with the message:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "..." nib but the view outlet was not set.'

So I opened up my nib file, and I see for the view that there are no referencing outlets set. However, I try to click and drag the circle for "new referencing outlet" to File's Owner, but it won't let me...what do I need to do to get my view to display?

Addi answered 21/1, 2011 at 20:35 Comment(2)
Try cleaning the build first.Kymry
Im my case, the real error was that I defined a method in viewController (- (void) setValue:(NSNumber*)value forKey:(NSString*) key) that override ViewController method causing the error. I solved renaming this methodDrier
A
65

The View Identity - Class Identity was not set. After setting it to the appropriate class, the issue was resolved.

Addi answered 25/1, 2011 at 21:3 Comment(1)
Hi @John, I have done this but still getting this exception.Alcock
L
1956

Here's what worked for me:

  • Open the XIB file causing problems
  • Click on file's owner icon on the left bar (top one, looks like a yellow outlined box)
  • If you don't see the right-hand sidebar, click on the third icon above "view" in your toolbar. This will show the right-hand sidebar
  • In the right-hand sidebar, click on the fourth tab--the one that looks a bit like a newspaper
  • Under "Custom Class" at the top, make sure Class is the name of the ViewController that should correspond to this view. If not, enter it
  • In the right-hand sidebar, click on the last tab--the one that looks like a circle with an arrow in it
  • You should see "outlets" with "view" under it. Drag the circle next to it over to the "view" icon on the left bar (bottom one, looks like a white square with a thick gray outline
  • Save the xib and re-run
Louisiana answered 18/6, 2011 at 11:43 Comment(12)
I faced this issue on cocoa too and as long as the view wasn't loaded / added to the view hierarchy, all view outlets were NIL. After calling addSubview or something similar all outlets were connected. The funny part of my story was that po viewcontroller.view.subviews showed the missing objects.Petras
This solution is no longer working in Xcode 7.3 as of step 4&5. Instead, in interface builder, Control-drag from the File's owner to your View .Caddaric
Thank you so much. I was restructuring my views (embedding a table view within a view) and this helped me realize the view outlet was set incorrectly to the child table view I had moved.Joanjoana
Didn't work for me (no outlets section in the connection inspector, thus no "view" under outlets to connect to), but +1 for the effort you put into your answer. :)Wont
this should be the accepted answer. I was missing the point I have to associate also the view object to the main XIB view. Thanks.Wardlaw
If you dont see "view" in the outlets you can also ctlr-drag from files owner to the view on the storyboard. A contextual menu should appear with a "view" optionTomasz
@WilliamT.Mallard I have the same behavior. MyViewController is a subclass of subclass of UIViewController and there is no view under "Outlets". If I subclass MyViewController direct from UIViewController the view appears unter "Outlets". Magic or bug of Xcode.Canister
@1800INFORMATION Please don't copy parts of answers into other answers. That is not fair.Agc
@LightnessRacesinOrbit why not? if it makes the answer better then I think that's better for everyone. Kind of hilarious that you reverted a change I made 3 years ago just sayingSlander
@1800INFORMATION The information exists elsewhere on the same page. Copying it into someone else's answer without clearly displaying attribution is not only pointless, but it is also unacceptable. We do not need to merge all the answers into one - the model is to have multiple answers with their own angles that you can vote on for the best. Doesn't matter how long ago it happened.Agc
@LightnessRacesinOrbit good job making the top answer worse for everyone. Seems about right for SO these daysSlander
@1800INFORMATION Sigh. Way to not get it.Agc
C
265

This is Josh Justice proposal, but in a graphical way (pictures are mine):

  1. Select File owner
  2. On right hand side panel select custom class.
  3. Enter the custom class name

enter image description here

  1. On right hand side panel select oultets
  2. Drag view outlet to view component

enter image description here

Finally the View Controller is instantiated with the rolling code:

        PTFilterUserVC *aFilterUserVC = [[PTFilterUserVC alloc] initWithNibName:@"FilterVC" bundle:nil];

        //OPTIONAL.This is how 'I' am interested in present the view controller.
        [self.navigationController pushViewController:aFilterUserVC animated:YES];
Capitular answered 5/6, 2015 at 9:8 Comment(1)
I missed the 5th stepShippen
J
78

I can generally fix it by remaking the connection between File's Owner and the view. Control-drag from the File's owner to your View (in IB) and select view from the pop-up menu.

Jellyfish answered 25/10, 2012 at 1:4 Comment(0)
A
65

The View Identity - Class Identity was not set. After setting it to the appropriate class, the issue was resolved.

Addi answered 25/1, 2011 at 21:3 Comment(1)
Hi @John, I have done this but still getting this exception.Alcock
P
33

Are you sure you have a UIView (or subclass) assigned to the "view" property of yourViewController?

Right click on "File Owner" in the left pane of the xib for yourViewController and verify that the "view" outlet is set. If not, set it to a view!

this will definetly fix the Issue

Polad answered 21/8, 2013 at 13:5 Comment(1)
Clear, concise answer.Borak
W
19

I ran into this problem in a slightly different way from the other answers here.

If I simply created a new xib file, added a UIViewController to it in Interface Builder, and set that UIViewController's custom class to my view controller, that resulted in the "view outlet was not set" crash. The other solutions here say to control-drag the view outlet to the View, but for me the view outlet was greyed out and I couldn't control-drag it.

the greyed-out View outlet

I figured out that my mistake was in adding a UIViewController in Interface Builder. Instead, I had to add a UIView, and set the Custom Class of the File's Owner to my view controller. Then I could control-drag the view outlet of the File's Owner to my new view UIView and everything worked as it should.

Woolley answered 9/12, 2019 at 15:26 Comment(1)
This! Just create the view and set the UIViewController as the File Owner. Thanks!Artichoke
T
16

I had the same issue with XCode 4.6.3. I had started out with a couple files named MySettingsView.h and .m but deleted them in favor of MySettingsViewController.h, but despite trying most of the hints mentioned here, it still kept erroring with,

2013-07-05 11:48:17.205 MyApp[39024:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "MySettingsView" nib but the view outlet was not set.'

It was evidently still "confused", trying to load MySettingsView.xib instead of MySettingsView Controller.xib. Maybe its "do what I mean" logic is too fancy.

So I worked around the problem by hardcoding the NIB/XIB name in MySettingsViewController.m:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:@"MySettingsViewController" bundle:nibBundleOrNil];
}
Trousers answered 5/7, 2013 at 19:54 Comment(0)
F
16

For me all the things stated here https://mcmap.net/q/53741/-loaded-nib-but-the-39-view-39-outlet-was-not-set were true but still it was throwing error, reason was I created a View class with name ABCView and then deleted it later I added a view controller as ABCViewController so somehow it was referring to old ABCView in new view controller, I had to delete the ABCViewController and add a new one with different name that solved my issue.

Thanks

Fugacious answered 13/3, 2014 at 16:58 Comment(1)
Still a bug in 2020.Nealneala
S
14

To anyone that is using an xib method to create a UIView and having this problem, you will notice that you won't have the "view" outlet under the connections inspector menu. But if you set the File's Owners custom class to a UIViewController and then you will see the "view" outlet, which you can just CMND connect an outlet to the CustomView.

Splendent answered 21/2, 2018 at 23:3 Comment(1)
Awesome. Thank you so much. This should be an accepted answer!Dual
D
8

My issue with this was caused by having a duplicate nib in the class folder that did not have the view set. xcode seemed to be choosing one nib for a build and then the other the next time I built the project. Just deleted the other one. Looks good. Doh!

Disingenuous answered 30/8, 2011 at 20:7 Comment(0)
H
7

Just spent more than hour trying to find out why my view property is not set in my view controller upon initiating it from nib. Remember to call "[super initWithNibName...]" inside your view controller's initWithNibName.

Hyperactive answered 28/7, 2011 at 13:57 Comment(0)
O
6

I just fixed this in mine. Large project, two files. One was "ReallyLargeNameView" and another was "ReallyLargeNameViewController"

Based on the 2nd answer chosen above, I decided I should clean my build. Nada, but I was still suspect of XCode (as I have two identical classes, should abstract them but eh...) So one's working, one's not. File's owner names are so far as copy and pasted, outlets rehooked up, xCode rebooted, still nothing.

So I delete the similar named class (which is a view). Soon, new error "outlet inside not hooked up" literally was "webView not key value" blah... basically saying "Visual Studio's better". Anyway... I erase the smaller named file, and bam, it works.

XCode is confused by similar-named files. And the project is large enough to need rebooting a bit, that may be part of it.

Wish I had a more technical answer than "XCode is confused", but well, xCode gets confused a lot at this point. Unconfused it the same way I'd help a little kid. It works now, :) Should benefit others if the above doesn't fix anything.

Always remember to clean your builds (by deleting off the simulator too)

Objurgate answered 21/6, 2012 at 0:49 Comment(0)
S
6

The previous answers almost solved the problem for me, but the last step was missing.

  1. Create a xib and swift file with the same name.

Add View and Swift File

  1. Set the file owner to be the UIView subclass.

enter image description here

  1. Drag an outlet from the View to the UIView subclass, name it "contentView"

add the contentview

  1. Add this custom initializer so when the xib loads it attaches the contentView

add the contentview as a subview

required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        Bundle(for: self.classForCoder).loadNibNamed("SampleView", owner: self, options: nil)
        addSubview(contentView)
        contentView.frame = self.bounds
        contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
    }

Now any @IBOutlets you add will be attached.

Cheers, Richard

Saddlebag answered 23/4, 2019 at 17:3 Comment(1)
Very nice, the third step was missing.Chromogen
O
5

I also had the same problem and my issue was that i added an other Localisation (English) to the ViewControllers nib so my App with the Localisation German could´t find the nib with the Localisation English!! Hope this helps anybody!

Overbear answered 11/10, 2011 at 13:17 Comment(0)
M
5

I had face the same problem while accidentally deleted xib reference and added it again.I just fixed by making connection between Files owner and the view.Also make sure that your FilesOwner's custom class is your expected viewController.

Miser answered 22/4, 2016 at 10:23 Comment(0)
E
4

For me, the problem was caused by calling initWithNibName:bundle:. I am using table view cells from a nib file to define entry forms that sit on tableViews. As I don't have a view, doesn't make sense to hook to one. Instead, if I call the initWithStyle: method instead, and from within there, I load the nib file, then things work as expected.

Extraterritorial answered 31/12, 2011 at 16:34 Comment(0)
C
3

I had the same problem, but a slightly different solution was called for. The problem in this case was the class of the File Owner, rather than the class of the View. To set this, I had to click the "backwards play" icon in the lower left corner of the Interface Builder window, and options then appeared that isolated the characteristics of the File Owner, the First Responder, and the View. Clicking on the first one (a large transparent box), enabled me to then set its custom class as suggested above.

Calcicole answered 4/5, 2012 at 2:16 Comment(0)
M
3

I had the same problem, but a different solution was called for. The problem in this case was the class of the File Owner was not connected to xib file.

Marienbad answered 5/1, 2013 at 17:18 Comment(0)
G
3

I ran into something very similar tonight, with a Swift UIViewController subclass. In this case, none of the above fixes worked, but reordering my code a bit did. Net-net, having an extension to the subclass occur before the subclass's definition itself in the same file seems to confuse XCode, despite compiling fine; the fix was to place the extensions after the subclass's definition.

I've posted the details in an answer to this similar question.

Galosh answered 22/2, 2015 at 6:3 Comment(0)
D
3

In my case , the designated initializer - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil in ***ViewController class was implemented so even if when I call other initializer to initialize the object ,the designated initializer will be called .

So to resolve this problem checking wether the - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil is also a proper way.

Diaphoresis answered 17/12, 2015 at 7:9 Comment(0)
B
3

My problem was in wrong classes. I used custom .xib for my custom view. Correctly it has to be set like here:

  1. View shouldn't have a class
  2. Class which is used with the .xib is set in File's Owner tab
  3. Outlets are connected to File's Owner and not to the View.

    pic

Blowzed answered 30/10, 2019 at 15:38 Comment(0)
A
2

Just had the same error in my project, but different reason. In my case I had an IBOutlet setup with the name "View" in my custom UITableViewController class. I knew "view" was special because that is a member of the base class, but I didn't think View (different case) would also be a problem. I guess some areas of Cocoa are not case-sensitive, and probably loading a xib is one of those areas. So I just renamed it to DefaultView and all is good now.

Attendant answered 30/10, 2012 at 19:41 Comment(0)
H
2

select the files owner and goto open the identity inspecter give the class name to which it corresponds to. If none of the above methods works and still you can't see the view outlet then give new referencing outlet Connection to the File's Owner then you can able to see the view outlet. Click on the view Outlet to make a connection between the View Outlet and File's owner. Run the Application this works fine.

Hippocampus answered 26/11, 2012 at 9:34 Comment(0)
H
2

In my case, the view was not viewed in xib. in xib the View was size = none (4th tab right hand). I set size to Freeform and reload xCode. view was appealed and I set the proper link to View.

Hypertensive answered 8/4, 2013 at 18:34 Comment(0)
M
2

If you have tried everything and you still get this error, try re-creating the class file from scratch but remember to select the "Also create XIB file" check box. This will auto link up a few items that are not linked when creating these files separately. After this is created, you can likely cut and paste everything onto the new XIB and it should work fine.

I am finding this issue specifically with creating files separately in Swift.

Masqat answered 10/2, 2015 at 18:11 Comment(0)
B
2

for me it happened, when

  • I have a ViewController class ( .mm/h ) associated with the Nib file,
  • UIView from this ViewController has to be loaded on the another view as a subview,

  • we will call something like this

    -(void)initCheckView{
    
       CheckView *pCheckViewCtrl = [CheckView instance];
    
       pCheckView = [pCheckViewCtrl view];
    
       [[self view]addSubview:pCheckView];
    
       [pCheckViewCtrl performCheck];        
    
    }
    

Where

+(CheckView *)instance{
    static CheckView *pCheckView = nil;
    static dispatch_once_t checkToken;

    dispatch_once(&checkToken, ^{
        pCheckView = [[CheckView alloc]initWithNibName:@"CheckView" bundle:nil];
        if ( pCheckView){
            [pCheckView initLocal];
            **[pCheckView loadView];**
        }
    });

    return pCheckView;

}

Here loadView was missing,,, adding this line resolved my problem.

Burdett answered 24/8, 2015 at 16:2 Comment(0)
M
2

I had the same problem, I figured out and it is because of i had ticked "Static cells" in the properties of Table View under Content option. Worked when it changed to "Dynamic Prototypes". Screenshot is below. enter image description here

Morty answered 19/9, 2016 at 6:13 Comment(0)
D
2

I had a similar problem with Xcode 9.3, and setting "Module" under the "File Owner's" attribute inspector to the project module fixed this for me.

Diametral answered 16/5, 2018 at 9:34 Comment(0)
P
2

Open your storyboard file where your viewController exists, or XIB related file with textEdit.app and check if the storyboard or XIB name is the same of your viewController, then change it, save and reload/restart Xcode.

Pinery answered 23/7, 2019 at 7:35 Comment(0)
S
1

If you're using a custom init method, check that you're returning something valid. I ran into a piece of code that crashed on something like this:

- (id)init {
   self = [super init];
   if (self) {
      CustomController *controller = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(className) owner:self options:nil];
   } return self;
}

In another class, the controller was created like so:

CustomController *controller = [[CustomController alloc] init];

The problem is that in the init method, self hasn't changed and should look like this instead:

- (id)init {
   self = [super init];
   if (self) {
      CustomController *controller = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(className) owner:self options:nil];
      [controller viewDidLoad];
      self = controller;
   } return self;
}
Saba answered 14/8, 2019 at 8:52 Comment(0)
L
0

I'd like to second Stephen J. Some times X Code does just get confused. I just had an experience where I had played around with the UI a lot, and had added and deleted outlets quite a few times. The outlets just would not wire-up any more. I never did figure out a specific reason (I had tried all the solutions above), and I just had to delete the NIB and recreate it from scratch, and in fact had to use a different name for the NIB before it would work. (XCode 4.6.1) Wasted a couple of hours on that.

Luxembourg answered 21/3, 2013 at 22:2 Comment(0)
T
0

When you have a Swift code that references different namespace with @objc, be sure to use the Objective-C class name:

@objc(NamespacedSomeViewController)
class SomeViewController: UIViewController {
....

In this case, you need to use NamespacedSomeViewController in the IB.

Tristis answered 16/7, 2019 at 19:21 Comment(0)
W
0

TLDR; If you are using xib for UIView, you can't have Foo, Foo.xib and FooController at the same time.

I encountered this problem even though I correctly set up all the outlets. Turns out, it was because I had a view with "almost the same name" as a view controller, and somehow the compiler ignores my class under Custom Class > class, and just assumes that I wanted to link the xib to the view controller.

For example, let's say I had a custom view MyActionSheet with MyActionSheet.xib, correctly linked and filled in the class name. At the same time, if I had a view controller MyActionSheetController, the xib file will always link to MyActionSheetController and cause the error, no matter what I set in Custom Class > class.

The way to solve this problem is to change the name of the view. For example, from MyActionSheet to MyActionSheetView.

Wild answered 19/1 at 5:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.