I' wondering how I would go about having two windows in interface builder and then being able to show and/or hide either of them from xcode with objective-c?
Any ideas?
Thanks
Make sure you've got outlets connected to each of your windows.
Then you can do - (void)orderOut:(id)sender to make a window invisible, or - (void)close if you actually want to close it.
To make the window visible and bring it to the front, do - (void)makeKeyAndOrderFront:(id)sender.
There's more information in the NSWindow class reference.
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.
This is my first Mac OS-X app, so this might be a stupid question.
In my app basically i have two windows,
The Main window which comes with MainMenu.xib file by default
I created another WindowController with another xib file.
I have created a AppController class which is connected to the MainMenu.xib, what i am trying to do in the awakeFromNib method in the AppController class is load either the main Window or the custom window, but load the Main Menu each time. Is it possible?
I couldn't do it the previous way, if i needed to use the custom window, i would first create the main window and close it immediately.
I tried to get the main window by this, [[NSApplication sharedApplication]mainWindow]; and then closing it. But was unsuccessful.
I dont think i have proper understanding yet with windows, views and controllers of cocoa. i am following aaron hillegass's COCOA Programming for Mac OS X book.
please suggest me some other tutorials so that i can understand this thing clearly.
You need to un-check the windows "Visible at Launch"
Then based on your BOOL value, you need to show the window and make it orderFront.
I'm having trouble figuring out how to change between different custom views in my XCode project.
I have my Main nib file, consisting of 1 main window, and 5 custom views. The main window consists of 5 buttons, all which need to connect to the different views. So for example I click on button 1 and it closes the current menu and loads custom view 1.
I'm having trouble figuring out how this would be done.
I guess I would create IBOutlets for the 5 different buttons and the 5 custom views, and connect them to different methods such as openView1, openView2, where each method would close the current menu and load the custom view?
Could anyone help me code-wise how I would achieve this?
Any help greatly appreciated.
Thanks
So essentially you want a tab view?
You can make an NSTabView in Interface Builder. Set the number of tabs to 5. Then lay out the contents of the views you want inside that.
If you are happy using the standard system provided visual look for your tabs, then you're done. If however you want to have custom buttons that switch tabs, read on.
With your tab view selected, set its style to Tabless:
This makes the tab buttons disappear. That means that switching between views needs to be done through code. First you'll need an IBOutlet that represents your tab view itself: connect that up. Then write an IBAction method for openView1:, that might look something like this:
- (IBAction)openView1:(id)sender
{
[tabView selectTabViewItemAtIndex:0];
}
Make yourself a button (that sits in your window somewhere outside the tab view, otherwise you'd only be able to access it from one tab!) and connect it to this action.
This is probably the easiest way to get going with an interface like this. There's a whole bunch of ways to improve on it depending on how you want to structure your code. For instance, it sounds like you're coming from iOS development, where you'd make a UIViewController for each tab. Well, on the Mac there exists NSViewController, so you can use a similar pattern: but if you do you'll need to write some code that handles getting your view controllers' views into your tab view. It doesn't happen automatically through Interface Builder like it does on iOS. This tutorial should get you started if you choose to go that route.
I am developing an interface for an OpenGL Simulation on the Mac using Interface Builder. All OpenGL stuff is being written in C++ (which I know fairly well). Unfortunately I have no idea about Objective-C.
Basically, I have a few NSTextField displaying info on an object selected on the Screen. Using these textfields the user is the able to manipulate the object on screen and then there is a Save Button and a Restore Button (which either save the new values or restore the original ones)
I have this all working. My problem is when I enter data into an NSTextField the "focus" of the windows seems to remain on the NSTextField (blue border remains around it).
I use the keyboard to interact with items within the NSOpenGLView, I need to pass the focus back to the NSOpenGLView when I hit either the Save or Restore buttons.
Sorry if this is a very straightforward question
Thanks
David
Have you tried using NSWindow makeFirstResponder method to make your NSOpenGLView the first responder?
Just managed to get it working.
I had to add the line:
[[NSApp keyWindow] makeFirstResponder: MyOpenGLView];
to the end of the function being called when I click on either of my buttons.
(Thanks Julio for pointing me in the right direction)
I'm working on an menulet-based application for the Mac that requires me to have a text field in the menu. I've searched high and low and have not found any examples of how to do this, yet I have seen many menulet apps that implement a text field. I've found no way to do it from Interface Builder, so I guess this text field would need to be defined and added to the menu programmatically.
If anyone could help me with this issue, it would be greatly appreciated.
An NSTextField is a view based subclass, therefore in code, when you create the NSMenuItem you must use the – setView: property and add the NSTextField to the menu item.
Also if you use an NSStatusItem, it also has a -setView: property.