I have been programming with LabVIEW for quite a while and wanted to do some programming on the Mac. That meant getting started with XCode and Objective-C. Getting my head trained to think in the development environment has been harder than I thought.
Some good resources:
- Become An Xcoder
- Cocoa Application Tutorial: Introduction to Cocoa Application Tutorial
- Cocoa Dev Central
MVC
XCode extensively uses the MVC design pattern. In this pattern, conceptually there are three parts to the software. The Model is your business data and whatever logic is important to keep it consistent. The View is the stuff that acts as the UI. (In the Mac world, usually this is built by configuring the UI in the Interface Builder environment.) And the Controller connects the two.
In some schools of thought, the controller has all the business logic that connects the changing of the data and the changes in the UI. This is not just updating of the UI, but the encapsulation of real value-added work done by the program. The View is just a presentation layer, and the Model is mostly a complex persistent state machine. However, the Cocoa way seems to move that business logic stuff into the Model domain, and the Controller is more simply an interface between the M & V.
However, in my experience, the Controller on the Mac usually keeps everything more up-to-date than on other systems. If you make a change in one place, it automagically is immediately updated everywhere it applies. This is courtesy of the way Cocoa's Controllers work together with the software architecture of the Mac.
How to do the graphical part of Cocoa without Cocoa Bindings.
In this case you will write both the controller and model classes. However, to connect to the graphical interface, there must be a proxy object in the .nib file corresponding to your controller. It will be set up with Actions and Outlets corresponding to the controller code. Using the IB environment, you will connnect the IB view to the proxy IB controller class. When the code runs, the actual objects loaded will be from the text code you wrote, but the run-time environment will know how to connect to it because of the proxy object.
- have or create an XCode Cocoa project.
- make the UI
- double-click the .xib file (opens Interface Builder)
- select "Window" (the target app's window) from the .xib window
- bring up the Info inspector panel
- under the Attributes inpector (cmd-1), change the app window name as desired
- the Library panel has control widgets under Cocoa > Views & Cells
- drag desired controls/indicators to the app_win
- resize & realign
- more app windows are available in Library at Cocoa > Application
- drag them to the .xib container window
- etc until done
- create a new Objective-C file to be associated with a graphical window (its controller)
- name it & save it (usually with "Controller" in the name)
- create outlets & actions
- declare an IBOutlet object pointer for each UI object you wish to communicate with
- IBOutlet will be removed by the preprocessor (it is a guide to the compiler with respect to IB connections). It does not serve as the type of the pointer.
- create a method with a return type of IBAction (equiv to void) for each action you wish to respond to
- declare an IBOutlet object pointer for each UI object you wish to communicate with
- create a NIB proxy object for the controller Obj-C object
- it acts as a proxy for the actual Obj-C object
- at run-time, the Obj-C object you wrote will actually be loaded
- the proxy is used to facilitate connection of Interface Builder things to ObjC classes during design
- Outlets (IBOutlet) are pointers to UI objects (usually widget) so as to send them messages
- Actions (IBAction) are pointers to event handlers (usually custom object methods) so as to send them messages
- in the Classes tab of the IB library window
- drag an object named exactly as your Controller class from the library to the .xib container window
- OR more traditionally
- tell IB about the controller class file you had written
- drag the icon of the class's header file to the IB window of graphics objects
- _OR_ go File > "Read Class Files..." and pick out the controller header file
- connect the controller text file to an IB controller object
- drag a (plain) object from the library to the .xib container window
- are available in Library at Objects > Cocoa > Objects & Controllers
- bring up the Identity Inspector (cmd-6)
- pick the class to be associated with the IB object from the list in the Class Identity section
- actions & outlets will be filled out from the header file
- they will also be updated as the controller file is changed and saved
- drag a (plain) object from the library to the .xib container window
- tell IB about the controller class file you had written
- it acts as a proxy for the actual Obj-C object
- make connections to actions & outlets
- you will connect widgets to pointer variables by dragging
- one-stop
- click on the controller object in IB window of graphics objects
- choose the Connections Inspector (cmd-5)
- drag from the little circle to the right of each item, to the desired interface widget item
- OR more traditionally
- the direction of dragging shows the direction the message will move
- a pop-up menu gives connection choices
- a confusing part: sometimes the pop-up menu is of items where the drag started
- crtl-drag (right-click drag) from widget to the proxy object
- pick desired action pointer from list
- crtl-drag (right-click drag) from the proxy object to widget
- pick desired outlet pointer from list
How to do the graphical part of Cocoa with XCode Binding.
This technology relies on key-value coding (KVC) and key-value observing (KVO) technology.
- have or create an XCode Cocoa project.
- create desired model classes as part of the MVC design pattern
- if you have accessor methods that follow conventions, independent variables will automatically become available
- for dependent properties, you need to create class methods to be used to notify key-value observers of changes
- create model NIB proxy object (plain NSObject)
- drag in from library
- add more detail...
- create controller NSObjectController object
- drag in from library
- add more detail...
Some software resources
- A regular expression framework installed like this.