_loadViewFromNibNamed called even though no nib associated with UIViewController - iphone

I have a UIViewController subclass which I am instantiating and trying to push on to the navigationController like so :
MenuVC *menuVC = [[MenuVC alloc] init];
[self.navigationController pushViewController:menuVC animated:YES];
I don't have a nib file associated with this class and so just doing a simple ..alloc] init] instead of initWithNib:bundle:.
I am still getting a crash on the pushViewController call and the trace says this
reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "MenuVC" nib but the view outlet was not set.'
I don't see why this could be happening. I have other view controller which loads fine with the same method.

I think I found the answer to this in Apple's Docs :
A view controller has an associated nib file if the nibName property returns a non-nil value, which occurs if the view controller was instantiated from a storyboard, if you explicitly assigned it a nib file using the initWithNibName:bundle: method, or if iOS finds a nib file in the app bundle with a name based on the view controller’s class name.
So, this means the OS actually tries to load the nib if it finds one. I did have a nib in my bundle with same name as VC but had not associated it's File Owner.

The real problem is that the Viewcontroller have a view that comes in default and somehow that connection of IB to class view->view went missing and hence the error

This might not be an "answer" but this was resolved. I just removed the whole file and added a new file with a different name and it worked.

Related

Issues Using .xib With Storyboard

Hey I'm trying to open a view controller that is designed using a xib interface file. I am using the following lines of code to generate the controller from within a view controller created as a storyboard component.
YourViewController *viewController [[YourViewControlleralloc] initWithNibName:#"ViewControllerName" bundle:nil];
[self presentViewController:viewController animated:YES completion:nil];
I have never set up a view controller with a .xib before nor have I ever linked to one via storyboard so anything could be wrong. Here is the error I am getting when I try to present the view.
2013-05-17 13:06:45.120 Coffee[8991:907] * Terminating app due to
uncaught exception 'NSInternalInconsistencyException', reason:
'-[UIViewController _loadViewFromNibNamed:bundle:] loaded the
"PeekPagedScrollViewController" nib but the view outlet was not set.'
Set the File's Owner in the .xib to the class of your view controller (perhaps it is called PeekPagedScrollViewController).
Then connect its view outlet to the main view in the .xib file.
It was connected properly I had just forgotten to declare an initWithNibName method and therefore it crashed when I called it from storyboard. Silly mistake but thanks for all the great feedback #matt
Set the Storyboard view controller custom class to your class name (the one your xib is also referencing), then in that same class, simply override initWithCoder with:
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [[super initWithCoder:aDecoder] initWithNibName:#"YourCustomViewControllerXibsName" bundle:nil];
if( self ) {
}
return self;
}
This will enable you to design layout and segue transitions in Storyboard, but design your layout in a standalone nib file.
Storyboard will handle instantiation, but when initWithCoder: is called, initWithNibName: will be the method that instantiates self via the xib.

Universal UIViewController

Please help me, I never made universal UIViewControllers with xib for iPhone/iPad. How I can create class with .m and .h files and _iphone.xib and _ipad.xib?
I was try to create xib manually and create outlets for view, but after pushing this controller I had uncaught error:
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "FullNewsViewController" nib but the view outlet was not set.
Thanks!
The problem, what I can see from the error you posted, is that you have not set the owner properties of the NIB correctly.
Your nib files does not seem to have the correct file's owner and thus can't set the view on the view controller.
Open the nib, and go to File's Owner in the left since of the screen. Set the Custom Class property to the class of your viewController.
The right click on the File's Owner again and drag the view property to your view.
You should follow Apple iOS App Programming Guide
Creating a Universal App
Here you will get some tips, how to create Universal Application.
Hope this will help you out.
REview this link may be helped you...
http://elusiveapps.com/blog/2011/10/converting-iphone-apps-to-univeral-ipad/
if the view controller can easily be used for both ....
MyViewController.h
MyViewController.m
MyViewController~ipad.xib
MyViewController~iphone.xib
Set the File's Owner in both the xib files to your UIViewController subclass and connect up the view + anything else you want connected.
When I init my view controller this is all that's required
MyViewController *viewController = [[MyViewController alloc] init];
// OR
MyViewController *viewController = [[MyViewController alloc] initWithNibName:nil bundle:nil];
The Apple docs for UIViewController state that if you provide a xib with the same name as the view controller it will be loaded. The ~ipad and ~iphone ensure that the correct xib is loaded.

Why does a .xib file initialize despite me initWithNibFile: nil

I created a new window based applications. Immediately i created a class as a subclass of ViewController and in the appdelegate didFinishLaunchingWithOptions method i've added the following code
TestView myView = [[TestView alloc] initWithNibName:nil bundle:nil];
[self.window makeKeyAndVisible];
[self.window addSubview:mainMenu.view];
[mainMenu release]
However, despite this it still initializes the nib. If i placed a button in TestView.xib it shouldn't technically show it right? I initialized TestView without a nib? Why does it show up?
If you pass nil to -initWithNibName:bundle:, it will attempt to load a nib from the main bundle that has a filename that matches the name of the subclass, or the name of the subclass without the "Controller" suffix.
If you dont want it from a nib you should not init it in this way, it will still search for a xib that matches the class name exactly.
From the apple documentation found here:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html
If you specify nil for the nibName parameter, you must either override the loadView method and create your views there or you must provide a nib file in your bundle whose name (without the .nib extension) matches the name of your view controller class. (In this latter case, the class name becomes the name stored in the nibName property.) If you do none of these, the view controller will be unable to load its view.

'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the GameView nib but the view outlet was not set

This is not the same situation as the multitude of other similar questions here.
* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the GameView nib but the view outlet was not set.'
You might be thinking "do as it says, connect the File's Owner to the View in IB!". But the thing is, I don't even HAVE a GameView.xib in my project or even in the project directory.
I do have a "GameViewController.m" and matching "GameViewController.xib" in my project. Using that GameViewController is what brings up this error, but I don't understand where it gets the idea to try and load "GameView.xib". Shouldn't it use "GameViewController.xib" instead?
If I grep my project directory, I do see it referenced from "UserInterfaceState.xcuserstate".
<string>file://localhost/Users/bemmu/Dropbox/b2/iphone/ValleyStory/ValleyStory/GameView.xib</string>
This mentioned file does not exist. I might have had a file with that name before and renamed/deleted it, but it's not being referenced to from anywhere that I can see in IB.
Did I manage to confuse xcode?
My solution was a little different.
Click on the xib in interface builder
Select File's Owner on the left
Open the File's Owner's connections inspector
If the view property isn't yet wired, control-drag it to the view icon (under the file's owner and first responder icons).
Check any nib files you're using (like MainWindow.xib). If you are loading GameViewController from a nib, check the file it's loading from (under the info tab in the inspector). Make sure it's set to "GameViewController" and not "GameView".
I had this issue as well, but had to solve it a different way. Basically, I have a view controller name MainViewController, which has a xib named MainViewController.xib. This nib has it's view property set to the File Owner which was MainViewController.
I also made a MainView.xib that contained a view that was going to be programmatically added to the view defined in MainViewController.xib and it's view. It basically encapsulated an internal view that would be in the MainViewController.xib's view, and also had it's File Owner set to MainViewController.
So basically, I wanted MainViewController.xib to load as the nib for the MainViewController object, and inside MainViewController, at some later point, I would add the internal view specified by MainView.xib.
A couple issues arose:
1.) I found in the Apple docs that when loading a view controller via storyboard or nib:
"If the view controller class name ends with the word “Controller”, as
in MyViewController, it looks for a nib file whose name matches the
class name without the word “Controller”, as in MyView.nib.
It looks for a nib file whose name matches the name of the view
controller class. For example, if the class name is MyViewController,
it looks for a MyViewController.nib file."
Therefore, you cannot have a nib called MainView.xib if you also have a nib called MainViewController and want MainViewController.xib to be the primary nib for MainViewController.
2.) Even if you delete MainView.xib or rename it to something else (MainInternalView.xib in this case), you MUST delete / clean your iOS simulator as the old nib file (MainView.xib) will still remain in the application. It doesn't overwrite the whole application package when you rebuild / rerun your application.
If you don't want to reset your content settings (perhaps you have some data you want to preserve), then right-click on your application in your iOS Simulator folder, Show Package Contents, find MainView.nib, and delete it. Xcode will NOT do this automatically for you when you rebuild, so we need to manually remove the old nib.
Overall, don't make nibs named MainViewController and MainView, i.e. nibs with the same prefix. Call MainView.xib something else, like MainInternalView.xib.
I recently solved this issue. Make sure you back up your project before following the steps given here (just in case). These steps solved my issue
Quit Xcode
Navigate to UserInterfaceState.xcuserstate located at .xcodeproj/project.xcworkspace/xcuserdata/<username>.xcuserdata and delete the file.
Reopen Xcode. Xcode will create a new UserInterfaceState.xcuserstate which will be clean.
In my case this error was produced by dumb mistake - I delete _view view
In my case, I was not using a xib at all. I needed remove the .m file from Build Phases > Compile Sources and added it back.
Given you referenced it previously it sounds like xcode hasn't ackowledged it no longer exists. From the Product menu select "Clean" and then "Build" hopefully this will get past the old reference for you.
Face the same Problem, had to change the view's name in code:
MyViewController *controller = [[MyViewController alloc] initWithNibName:#"WrongViewName" bundle:nil];
To
MyViewController *controller = [[MyViewController alloc] initWithNibName:#"RightViewName" bundle:nil];
I had multiple views, and by accident (I don't know how this happenned) but my background view didn't have a file owner, so for anyone else who has this problem in the future, make sure all your views have a file owner.
I was gettint the same error then check the classname from interface builder and see that I typed the view controller class name at the custom class attribute.
UIViewController searches for a nib with the same name as the controller when passed nil to initWithNibNamed:bundle: Check that the file name that you pass to the initializer is correct and exists!
For example:(e.g. [[CCVisitorsController alloc] initWithNibName:nil bundle:nil] then UIViewController tries to load nib with name CCVisitorsController as default.
If that file does not exist then the error you mentioned is thrown.
I had this problem because I was doing something bad in
(id) initWithCoder:(NSCoder *) coder
which the NIB loads.

was unable to load a nib named "TwitterDrilldownView"

-[UIViewController _loadViewFromNibNamed:bundle:] was unable to load a nib named "TwitterDrilldownView"
I get the above error when I push a new ViewController onto the navigation stack. This is the push code,
[self.navigationController pushViewController:[[[TwitterDrilldownViewController alloc] initWithTwitterAnnotation:temp] autorelease] animated:YES];
Basically I am just pushing a newly allocated and initialized view onto the stack. The init method of the ViewController is,
- (id)initWithTwitterAnnotation:(TwitterInfo *)aPOI {
if(self = [super init]) {
poi = aPOI;
}
return self;
}
As you can see I do not use any initialize with nib method and there is no nib file named TwitterDrilldownView in my project.
I did have a nib file before I created the TwitterDrilldownViewController called TwitterDrillDownView but I was using it to test a layout and, again, never used it. When I created TwitterDrilldownViewController the TwitterDrillDownView.nib was present in the project and it was after this stage that I deleted the nib.
The only cause for this problem that I can think of is that Xcode somehow created a dependency on the nib file because the nib file and view controller are named the same(TwitterDrilldownView.nib, TwitterDrilldownViewController.m), as if it was trying to be helpful but is ultimately messing up my project.
I have tried deleting and recreating the view controller in the hope that any references will be destroyed, and removed any reference to nib files in the project but to no avail.
Has anyone please got any experience with this problem or know a possible solution?
This also happened to me after deleting a XIB file out of my project. However, I did some messing around and was able to resolve the problem.
The key point is that Xcode seems to keep some kind of reference somewhere as you create XIBs and outlets/actions within them, and deleting a XIB file manually simply orphans these references. Removing these connections one-by-one, using the interface builder (UB) and the XIB seems to de-reference them properly.
The solution is then pretty clear:
Re-create a XIB file of the same name and similar construction (including the outlets and actions you had before). (Note: If you can't remember how to re-construct the XIB's outlets and actions, just re-create the XIB file with the same name and skip to my 'if it's still failing' note below.)
Manually, one-by-one, remove all of the outlets and actions in your XIB using interface builder (IB).
Re-build the app; if it works now, you can delete the XIB.
(Note: If it's still failing, you will probably have a different error. If the error refers to key-value compliance then it will list a method in the error - and that method is your problem. Make sure you recreate the named outlet/action in IB, and remove it manually using IB. That seems to de-reference the call in Xcode/your build environment.)
Happy hunting!
UIViewController should be initialized using initWithNibName:bundle: method. In its description stated:
This is the designated initializer for
this class.
If you specify nil for the nibName
parameter and do not override the
loadView method in your custom
subclass, the default view controller
behavior is to look for a nib file
whose name (without the .nib
extension) matches the name of your
view controller class. If it finds
one, the class name becomes the value
of the nibName property, which results
in the corresponding nib file being
associated with this view controller.
So if you do not load you view controller from nib file make sure you override loadView method and set controller's view property in it.
Hope that will help.