Reference main NSWindow in AppDelegate using Storyboard? - xcode

I am attempting to set an outlet for the main window of my app in my App Delegate, then connect it within Interface Builder. I create the outlet within my App Delegate easily:
#IBOutlet weak var mainWindow: NSWindow!
However there's no way, within interface builder, for me to connect a referencing outlet to the App Delegate. Instead, I can only connect it to the Window Controller, which I hope this picture shows:
The first object is the Window Controller and the second object is the First Responder, however the App Delegate object is missing. The menubar has the App Delegate object:
And I can connect anything from the menubar to any outlets in the App Delegate.
I figure I can access the window object by using:
NSApp.windows[0]
But that seems prone to error, especially if I have more than one window.

I dont know if this is correct way of doing, but this will solve your problem.
Decalre a NSWindow property in AppDelegate
weak var window: NSWindow!
and set the property in something like windowWillLoad of the NSWindowController
(NSApplication.sharedApplication().delegate as! AppDelegate).window = self.window
You will have to subclass NSWindowController to define windowWillLoad

Related

storyboard viewcontroller to coded viewcontroller

Is it possible to connect Storyboard ViewControllers with manual code-written ViewControllers? What I'm trying to do is make an app that, when internet connection is established, I see the viewcontroller made in the storyboard with tools given in the xcode (like the stepper, progress view, and etc) and when there's no internet connection, I want to access my coded viewcontroller that doesn't use the provided storyboard. How do I make the transition from the viewcontroller in the storyboard to the one not in the storyboard? Thanks!
UPDATE:Image depiction of what I seek to find:
depiction
I believe what you wanna do is show a storyboard programmatically?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "ViewController")
window?.rootViewController = vc
Don't forget to add the "ViewController" identifier to the ViewController in your Interface builder.

Navigation Controller crashing app

Within my app I have a UIViewController and a UITableViewController.
I wish to navigate from the UIViewController to the UITableViewController.
Within the UITableViewController I want a navigation bar so that I can 'add' items and declare when I am 'done' so that I can travel back to my UIViewController.
To achieve this I have embedded the UITableViewController within a UINavigationController as this reveals the navigation bar which I can add my buttons to and add a title.
I have 'control dragged' a segue from the UIViewController to the UINavigationController, which itself is attached to the UITableViewController.
UIViewController -> UINavigationController -> UITableViewController
My intended functionality is to travel from the UIViewController to the UITableViewController. However, the app crashes with the following message:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Storyboard (<UIStoryboard: 0x7f96c1451890>) doesn't contain a view controller with identifier 'UINavigationController-IxO-nE-zon'
The app does not crash if my segue is directly attached from the UIViewController to the UITableViewController (with the UINavigationController gone). However, then I do not get my Navigation Bar at the top of the UITableViewController which I need.
So my question is either: can I fix this error so that I can travel between them, or is there another way of doing this so that I can travel between them whilst keeping my navigation bar.
Thank you!
Okay, turned out I had the setup correct but I required a project clean before it worked. I guess because I added the storyboard ID's after adding the segues.
Thank-you to the following answer: Answer

How to access OSX NSApplication from StoryBoard created default ViewController?

I’ve used Xcode 6.3 to create a very simple Swift Mac application. It is not a document-based app. My app has one NSViewController, and it loads, opens and runs as expected. However, now I need to use the Application menu, and I don’t see the proper way for my NSViewController to communicate with the NSApplicationDelegate or the NSApplication itself.
I am looking for some chain of properties or methods I can call from my NSViewController, after it has loaded, but before it is displayed.
Try
override func viewDidLoad() {
super.viewDidLoad()
let myApplication = NSApp as! NSApplication
let myAppDelegate = myApplication.delegate as! AppDelegate
}
Works as expected within NSViewController instance.

How does window management work in Mac development for MenuBar apps?

I'll preface this by saying this is my first Mac application, though I've been building iOs apps for some time.
I've got a menu bar app (system tray app) by which I mean I've got this.
I now want to show an NSWindow I've created in Interface Builder so I've created a class that's derived from NSWindow. Made my class the Window's delegate and from the App Delegate I do this:
MyClass *myClass = [[MyClass alloc] init];
[myClass display];
[myClass center];
[NSApp activateIgnoringOtherApps:YES];
[viewer makeKeyAndOrderFront:nil];
This seems to show a window without the standard window buttons (minimise, maximise, close) rather than the window I've defined.
Is this the right way to be showing windows? and how should the Window be defined so it shows my designed interface?
In Windows Forms programming this would be:
Form myForm = new Form();
myForm.Show();
You rarely need to subclass NSWindow unless you want to override a window's behavior. A more typical usage scenario would be to use an instance of NSWindowController or a subclass of NSWindowController to manage the window, by making that class the File's Owner in Interface Builder. Once this is done, to grab an instance of the window use:
NSWindowController *wc = [[NSWindowController alloc] initWithWindowNibName:#"NIBNAMEHERE"];
[wc showWindow:nil];
Another alternative in your app would be to add an NSWindow IBOutlet to your app delegate, and load the window in your app delegate with:
[NSBundle loadNibNamed:#"NIBFILENAME" owner:self];
[_window makeKeyAndOrderFront];

How to use NSWindowController?

I'm looking in to using NSWindowController and I just can't think how to get it working. How do I start using it?
It's difficult to answer this question without knowing what you're trying to do. However, if you are writing a document-based application, an NSWindowController is automatically created for each window you create, so you don't need to create one specially.
The way I use NSWindowController is I create a different subclass for each type of window in my application. For example, I might have a 3D application with an AppWireframeWindowController and an AppPreviewWindowController. Each subclass automatically loads the correct nib file, and has code that hooks the document's data to the views in the nib.
If you are using storyboards you can connect an NSWindowController subclass up in IB. Otherwise if you are using nibs and have just the default template for a Mac Cocoa app then you may need to make it in code or just use a subclass of NSWindow.
Otherwise you can create a new NSWindowController and check the 'Also create XIB file for user interface' and it will give you the nib and also the NSWindowController subclass. It is basically a new nib where 'File's Owner' is your NSWindowController and the Window is the .window object inside the NSWindowController and the delegate is also pointed there.
You may be able to modify that.

Resources