View outlet not available to File's Owner - cocoa

I was getting the following message upon creating a new view controller. Everything was compiling a-okay in Xcode without errors, but the app was immediately quitting upon loading the new view from a RootViewController.
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "HomeView" nib but the view outlet was not set.'
To resolve this, I tried connecting the File's Owner to the view, but the view outlet wasn't even available to the File's owner.

Moved from question section, per #Tim Post's suggestion.
Discovery:
I finally realized that when I added this View XIB, the File's Owner had been set to NSObject instead of HomeViewController (my newly-created view controller). Once I set the class identity in the indentity inspector, the view outlet was then ready to be connected to the view.
The app now loads and the new view loads perfectly. Just sharing for the benefit of the SO community.
Conclusion:
When creating a new View XIB, make sure to set the class identity to the desired view controller in the indentity inspector (Cmd+4). It may be set to NSObject by default.
The view outlet should then be available.

Related

Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' in Xcode 11

Cannot initiate Main.storybard as the first screen.
Question:
Failed to instantiate the default view controller for UIMainStoryboardFile 'Main' - perhaps the designated entry point is not set?
Workdone:
I tried to set the Main.storyboard as the initial view controller.
In the Info.plist, I also set the Launch screen interface file base name as Main
Result:
Nothing change. Problem still there.
Image:
Storyboard attribute setting
Info.plist setting
have you tried creating a new view controller and setting it as the initial view controller? I think it "jumpstarts" the project. Worked for me

Xib labels referenced in another viewcontroller

This is the error message I get:
Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key nameLabel.'
The below explains how it differs from the other questions posted. Since I am actually following the instructions from the link provided.
I have linked the Xib as follows:
The ViewController I would like to use is MapViewController.
The Xib is MarkerInfoView
The file owner is MapViewController and it's also where I have created the outlets.
i think that this happens becouse when you register your xib with a fileowner it maybe recreated and the outlets are disconeccted automaticlly.
i don't use that way of registring nibs
For using xibs initialize with MapInfo(nibName: "XibName", bundle: nil)
Other possible thing is that your outlets actually connected to mapinfoview, not controller. Delete them and recreate onto your viewcontroller

Outlets not showing in file owners

I create a class which subclass of UITableViewCell with xib created, when running , error shows:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[UIViewController _loadViewFromNibNamed:bundle:] loaded the "SYGPlayerTableViewCell" nib but the view outlet was not set.'
So I try to connect the view outlet , but I found the outlets not showing, It is really wired since it is ok in another file.
--problem resolved by change File Owner's custom class to "UIViewController"
In that case you are instantiating a UITableViewCell, there's no File Owner and the outlets go directly to the cell instance, named Player Table View Cell in your example.
'File owner' references the object that is instantiating the XIB, usually is a UIViewController, that instantiates its view in the XIB and references all the outlets to itself using the 'File owner'. But that doesn't make sense if all the objects are declared directly in the XIB.

What is causing the compile error: Illegal NSTableView data source?

After some 'cleaning' of a working app, the main window appears as designed in the MainWindow.xib.
I deleted a MainWindowViewController object in IB. (was beneath Font Manager)
MainWindow.xib has it's Custom class set to MainWindowViewController
The table uses Content Mode - View Based
The two required methods ARE included in that implementation file
included in header file
Table view connection has been made to File's Owner
EDIT: Does the full error message give a clue? Why the NSApplication?
*** Illegal NSTableView data source (<NSApplication: 0x100607ad0>).
What am I missing? What should I check to resolve?
Image included to help visualize.
EDIT 2: (New info)
I deleted the data source & delegate connection from the table view and the error disappeared. The same IB designed window opens with an empty table view.
My 'project' is more complicated than the documentation covers. How to add a Window Controller to the Cocoa/Core Data template and utilize this controller is not easily found.
The most pronounced symptom was that the code in the intended controller was not being executed. The 2 required delegate methods were present. awakeFromNib wasn't being called either.
After more research, it appears the 'missing link' is the (previously deleted) NSObject set to the custom class of MainWindowViewController or MainWindowController in the nib file. I'm still looking for why this needs to be or why connecting to the File's Owner doesn't make the connection.
On the one hand, we can set the File's Owner to the custom class (MainWindow)ViewController or (Main)WindowController and connect the contained views to File's Owner. This doesn't work as I expected.
On the other hand, (apparently correct) we can add an NSObject to the MainMenu.xib file, set it's custom class to the (MainWindow)ViewController or (Main)WindowController and then connect outlets to the subviews to this object. The step of adding this object in IB is the only way I could find of connecting the window to the controller.

How to connect a menu item to a custom action defined in the NSApplication delegate across nib files?

In my Cocoa app I have two NIB/XIB files that I need to connect:
MainMenu.xib: contains a custom object for the NSApplication delegate object and connects it to the proper outlet in the NSApplication placeholder.
ContextMenu.xib: sets up an NSMenu; one entry is supposed to open the preferences dialog
My custom app delegate defines an IBAction to bring up the Preferences window for my app.
How can I connect the NSMenuItem (second NIB) for showing the preferences to the action defined in the application delegate (first NIB)?
The Docs say this is supposed to be easy, but they fail to mention how exactly to do this in Interface Builder:
If the menu item refers to an application-level command, you can
implement that command directly in the application delegate or just
have the delegate forward the message to the appropriate object
elsewhere in your application.
I somehow need to access the app delegate in the second NIB, tell Interface Builder that it is of my custom class (so it knows about the custom IBAction), and connect it to the action of the menu item.
Thanks for any pointers!
If the other objects are in the responder chain, then you can just hook the action up to the first responder.
Notice the "if", though.
As Maurice Kelly mentions, your App Delegate is already part of the responder chain, so you can use that: Define a custom action on the First Responder (in Interface Builder) and a corresponding action on your App Delegate. If you have many actions that could clutter up the App Delegate, though, so you might want to use this architecture only for simple apps.
For bindings in the second NIB, application delegate bindings can be accessed by binding to Application with a model key path starting with delegate. ie delegate.managedObjectContext
A warning: Do not create an app delegate object in the second NIB. If you do, you will end up with a second app delegate instance with a second, separate managedObjectContext. The NIB creates another app delegate object. This is terrible.
In the loading of MainMenu.xib, [NSApplication sharedApplication] gets its delegate set to the delegate object instantiated in MainMenu.xib. If you create a delegate object in another NIB, you will have a delegate object that doesn't match [NSApp delegate]. (And you will smash your head into the wall trying to discover why the view of the context doesn't update)
Your second NIB will have a File's Owner which you should set to a class that is instantiated by your application. Within this class you can create a reference to the App Delegate which can be filled in when the class is being instantiated (e.g. using setAppDelegate:self if you are creating it from within the delegate).
Create an IBAction in this class which simply passes the action on to the App Delegate:
- (IBAction) passItOnAction:(id)sender {
[appDelegate openPreferences:sender];
}
I got the same problem, and solved it like this:
I created only one menu (MainMenu.xib). Since menu items for which the first responder does not provide any action are automatically greyed out, the items that are meant for the Document window will be greyed out when the Prefs window is activated.
I created a PrefsWin.xib for the definition of the prefs window. The File's Owner of that xib is a class named PrefsWinController, which inherits from NSWindowController.
The actions in MainMenu.xib simply connect to the First Responder rather than the delegate. If your instance of <NSApplicationDelegate> is an instance of NSResponder, the delegate will automatically act as the last responder in the chain. (I don't know where this might be documented, but it certainly works in my project.)

Resources