NIB Loading, Class Creating, and other Xcode 3 fun

Discussion in 'Mac Programming' started by Vortec4800, Apr 15, 2008.

  1. macrumors member

    Hey guys. I'm new to Xcode and Objective C and I'm writing my first application, and learning how everything works. I have decent experience in C/C++ and quite a bit of experience in Java and Eclipse now, and so far everything has been coming along quite nicely. I do have a few questions though, and I was hoping some people on here could give me a hand.

    My application is pretty simple, but I'm using it to learn how to do more complex things in the future. So far I have a table view and two buttons in the MainMenu.xib file, where I see a list of custom objects. I'm pretty well versed in object orientation and how to implement them in C, so I've got no problems there. The two buttons are to add and remove an instance of this custom object, and show everything in the table. As per Apple's recommendation in their documentation, I created a new xib file for the add window, where the user fills out a bunch of fields and presses a go button at the bottom of this second window. The second window closes and the new object instance is saved to an array of these custom objects and then of course shown in the table.

    Or at least that's what I'm going for. So far I have both windows ready to go, they look great. I've got some basic functionality going (some fields enable/disable depending on the option selected in a popupbox and so on) in each window but I need help joining all the parts together. Right now, the add and remove buttons don't do anything. I would need the add button to load this second window nib file and display it, and then the go button in this window would need to send a copy of this object to the array in the first window. Would I need to use an arraycontroller to keep track of this? How could I get the two xib/nib files to talk to each other and send objects back and fourth? I'll need an edit button as well that pulls up a version of this same window, but has all the fields already filled out with the values from the already created object for editing.

    I'm so used to coding everything out and not using something like IB to do most of the "glue code" as they call it for me. While I much prefer the Apple way of doing this, I just don't know how it all works together. Hopefully there are some people on the message board here with enough patience to give me a hand!
  2. Moderator emeritus


    For your add/edit window, if the forms are identical, with maybe a button label that is different, I'd use the same nib file for both. Just have a property "isEditing" and update the UI depending on what this value is.

    As for how to code it, I'd subclass NSWindowController for the add/edit window (put into its own separate nib). NSWindowController has several initWithWindowNibName.. methods you can call for loading the nib. Then in your main window, just initialize the window controller and setup a method to call that allows you to pass the necessary data to it. As for calling back to the main window, you can do it via delegates or notifications.
  3. macrumors member

    Alright sounds good so far, I was thinking about using the same window for the add/edit like you said so I'm glad to hear I'm on the right track.

    So I would need to subclass 2 NSWindowControllers, one for each nib file, use the controller in the main menu to call and start the add/edit window using the initWithWindowNibName method and that would start up my add window? What would the controller in the add/edit window do? Can that take care of my object creation when going back to my main window? What exactly are delegates and notifications?


Share This Page