I have a cocoa application. With two NIB/XIB files, one of them is the main (first) window that opens. The second NIB/XIB has some extra windows there.
How do I, for example, in objective c link one NIB button to another?
I know this is a very simple question, but I just can't find the answer.
You link (via IBAction) the action of a button from the first window (xib) to some Objective-C method.
This method contains code that does whatever you need with objects from the second xib.
(e.g. it might use NSBundle to load the second xib and bring forward one of the windows, etc).
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 am building an iOS app and i have most of the code/GUI built (its a single view app). Now i have the .xib and controller.h file next to each other and I want to associate a button click with a certain action. Everytime I try nothing happens. The UI indictor that I am dragging shows up but it wont let me connect to the method. This happens for all the objects/methods.
I have an almost identical project that works just fine too!
Select your XIB file and then select "Files Owner" which is the yellow cube.
In the Utilities Sidebar, select the Icon with the circle and arrow inside of it (which is the "Connections Inspector").
You can then Control + Drag your to your button.
//// Edit ////
After investigating your project...
Pretty sure you deleted a storyboard and then added a .xib but didn't set it's class, or link it's view as an outlet.
To do that:
Select the new view (XIB), then selct the main view, and in the identity inspector, set it's class to your custom class name "AVYSViewController."
Switch to the connections inspector and Control + Drag the "view" outlet to the main view in your XIB.
Also, your project was looking for the MainStoryBoard.storyboard file. So get rid of that setting by selecting your project, then selecting your target, and in the "Summary" tab, delete anything in the "Main Storyboard" field.
Note: I checked to see if your project settings were valid before I deleted the storyboard setting and it returned NO errors, so beware of this problem for future projects.
I think i'm reading the same book as you (Big Nerd Ranch's "iOS Programming"). If so then yeah, I had the same issue as you. Admittedly I am using my laptop to vnc through to a mac box to work on xcode, so I thought it was vnc not capturing the keyboard properly, but turns out it wasn't vnc as I could right-click/cntl-click drag on other things.
Admittedly again I didn't do much research on why it wasn't working, but in the meantime I just right-clicked on the button, which shows the events that the button emits, then click-dragged the "Touch Up Inside" event and dropped it on the controller/owner, which then displayed the actions/methods to hook the event to. If you look on page 17 (if you are reading the same book) then that is the event that was hooked up.
Sorry if there are any obvious glaring errors with what I just say, have only just started reading this book today after a big lunch.
I think you should rephrase you question.. It's very confusing. But if i understand what you are saying, you now have the files open side-by-side in the using the assistant editor. The Xib file on the left hand side, the .h file on the right. correct?
Now you are trying to link the event in the control, to the method you have defined in the .h file. right?
So if you are in fact seeing the methods in the .h file that you want to link to, but the editor is not letting you link to it, then chances are that you have have the xib file side-by-side with the wrong .h file. Are you sure that the .xib file and the .h that are side-by-side are both called controller ? controller.h and controller.xib ?
Just a newbie with iPhone development.. I just have a small problem but huge confusion with regards to NIB files. In Cocoa, how can we change the File's owner and First responder of a nib? And BTW, when can we actually say that we need more than one NIB file for a project in the first place?
Change the class of File's owner in Interface builder (select File's owner and open Identity inspector). You don't need to change anything in First responder.
As to your second question - you can build an entire project programmatically without nibs. I build mine with nibs for every view, controller, custom control etc.
Open the nib, click on "File's Owner"
Select the third inspection tool, "Identity Inspector" (?)
Type in the class this nib needs to be associated with.
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.