Forgive me if this is obvious, but I'm very new to Cocoa and Objective-C.
I have a very simple Text Editor app that has two windows created using Interface Builder. One for the text content, and another window with various controls and an "Enter full screen" button.
I have an IBOutlet called myWindow defined in the App Delegate, linked to the text editor window. I use:
[self.myWindow togglefullscreen]
in the App Delegate to enter full screen mode when "Enter full screen" button is pressed on the other window.
Now, I am trying to convert my application into a Document Based Application.
My question is: how do I access the current Document's window so I can call togglefullscreen on it, from within the App Delegate? Is this even possible/elegant?
Interface builder won't let me connect the IBOutlet in the App Delegate to the Document window in Document.xib which I guess makes sense as there could be multiple instances of it.
Another question that stems from this is, should this full-screen functionality be implemented in the Document class, and should the "Full Screen" button be on the Document window itself (as only one document will ever be full-screen)?
Any ideas would be much appreciated :-)
John
An NSDocument (sub-class) can have more than one window associated with it. Your NSDocument sub-class can access NSDocument's windowControllers property to get an NSArray of windowControllers (IIRC back to front order) and ask each one for it's associated window (property).
Related
I have two window controllers (with their own view controllers) on a storyboard.
In one window, I have the main program, a basic text editor with an NSTextView. In the other window, I have a single button.
I found out how to get the window to display by linking it to a menu item. It works.
The main window is linked to my ViewController class by default. The second window is also linked to the ViewController class and has its button linked to an IBAction in the ViewController class.
I have some simple code in the IBAction that basically tells the NSTextView to change its font size to a much bigger font. I have confirmed that the code itself works when called in other methods.
The button works, BUT it is using an entirely different instance of my ViewController class. So in result: the text size doesn't change.
So my main question here is how do I get an IBAction in one window to affect an object in another window.
I hope I did an alright job at explaining myself. Keep in mind this is my first Stack Overflow question:) I tried my best to research this question but mostly found information on iOS development and using XIB files.
It sounds like you have two windows with the same controller class but want what happens in one window to affect the other window. The easiest way is going to be with notifications. When the button is clicked in one window a notification gets posted that all instances of ViewController receive and respond to by changing the font size as needed. You could also look into setting a user default when the button is clicked and using bindings to keep the text field's font size tied to the current default.
Short answer is inside the title :)
Explaining: in my MainMenu.xib I have only the Main Menu of the application, that must be same for all NSWindows I open. There is one particular NSWindowController that has, let me say, all answers about when menu item must be enabled (via cases on selector in validateUserInterfaceItem) and what to do with all actions. When NSWindow associated with that NSWindowController is currently focused, there is no problem, but as I focus on another NSWindow all menus are grayed.
I have this flow now: MainMenu is created by reference to it as Main nib into info.plist, then AppDelegate do some init stuff and create MainWinController with MainWindow, at some point MainWinController creates 1+ DetailsWinController with DetailsWindow. AppDelegate manage my custom menu by calling each time functions from MainWinController.
So, how can I force the responder chain to query always that particular NSWindowController (MainWinController)?
You don't give many details to go on, and it isn't clear what you are trying to achieve.
My first reaction is that if you want share Menu items why are you creating multiple windows rather than Views within your MainWindow?
Assuming both your MainWindow and DetailsWindow implement the same selectors you could direct Menu Actions to the First Responder
To add the DetailsWinController in the InterfaceBuilder drag an NSObject from the Object Library, then in Identity Inspector change its class to your DetailsWinController class.
If your Main Menu has different items for the DetailsWindow just connect these to the actions in that instance.
NOTE if you so this you should NOT create the DetailsWinController in code.
If you really want to do this in code you will need to add actions and targets to your menu in code.
I was wondering how to remove the proxy icon in the bar of my mac app. I've added an image so you can see what Icon I'm talking about
Thank you in advance!
The icon is included in the titlebar of the application automatically when you've created an NSDocument based application.
You can remove the proxy icon by returning nil from the -[NSWindow representedURL] method. This could be accomplished by using a custom NSWindow subclass with the method overridden; or simply setting the property to nil at the appropriate times.
Be aware, you might loose other functionality you normally get for free by changing this behavior, such as the dirty/clean indicator for the window, or some prompting to save when closing the window.
Alternatively, if you wanted a different image, you could use:
[[NSWindow standardWindowButton:NSWindowDocumentIconButton] setImage:customImage]
Then implement -[id<NSWindowDelegate> window:shouldPopUpDocumentPathMenu:] to return NO to prevent the popup menu from appearing.
If your application isn't actually document based, or the window doesn't represent a document, consider refactoring to present this window a different way, rather than being a document window.
There is some additional information in the Cocoa window documentation.
When you create a new application in Xcode, it automatically creates a AppDelegate and a MainMenu.xib. The latter also contains the application main window, which is linked to the AppDelegate as an IBOutlet.
What I tried to do is, use a MainWindow from a different xib-file. However, there's absolutely nothing I can do to prevent Cocoa from showing the first window it created in the first place, even if I remove the IBOutlet link and comment it out in the source file and what not...
Hope someone can explain this, as it has been bugging me for a while now...
Whether or not a window in a XIB is shown at launch is not controlled by an explicit code, but instead controlled by the state of the window "freeze-dried" in the XIB.
More concretely, in an inspector of an NSWindow in the Interface Builder, you have the option called Visible at launch under the heading Behavior.
When the Cocoa system loads a nib and encounters a window with this bit on, it just shows it on the screen. It's independent of whether you have IBOutlet or not. You see, it's also the Cocoa system which sets UI objects to IBOutlets when it loads a nib... it can do whatever it wants.
I need to create many Cocoa items programmatically and most resources I can find focus on creating GUI with the builder. Is there any way to get the objective C code for an interface item created using the interface builder in Xcode? Also, I am a little confused about PopUpMenus. Are PopUpMenus contained inside PopUpButtons or are the independent? Also, can anyone link me to a good description of how to create PopUpMenus programmatically Using Cocoa and Objective-C?
It depends on what you mean by "PopUpMenus". In Cocoa a menu is a menu, specifically an instance of NSMenu. NSPopupButtons have an associated NSMenu object which you can assign to the button using the -setMenu: method.
You can certainly construct menus programmatically using the NSMenu and NSMenuItem classes. Take a look at the various -addMenuItem: methods in the NSMenu documentation.
You can also access the components of a menu created in Interface Builder programmatically if you have a reference to the menu, specifically an outlet instance variable that is connected to the menu in Interface Builder.
If you're referring to contextual menus that appear when you click the right mouse button, all you need to do is connect your menu to the view/control you want to attach it to with the -setMenu: method. You can make this connection in Interface Builder also.