13.07.2015 Views

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

Beginning Objective-C pdf - EBook Free Download

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

194CHAPTER 7: User Interfaces: The Application KitSo when setting a button’s appearance or key-equivalent, it’s actually an NSButtonCell thatimplements this logic, not the NSButton class itself.The reason for this separation is largely down to history. When the Application Kit was first designedin the late 1980s, computers typically had up to 1MB of video memory and less than 4MB of RAM.Views, with all their associated graphical state information both within the application and within thesystem’s window manager, required a lot of resources. Thus when a table or a grid (represented bythe NSMatrix class) was placed onscreen, it was highly uneconomical to represent each of theseitems using a separate view; the memory requirements would be massive for the time. As a result,many of these “view collection” classes actually utilized a single view, which would use a singleNSCell object to represent different pieces of content. As each control was rendered, the cell wouldbe updated with that control’s data and told to draw itself at a particular location within the view.This is how tables were implemented prior to OS X 10.7; each table contained a view for each of itscolumns, and each column used a single cell to draw its content on each row within that column.When the column contained text on each row, an NSTextFieldCell would be used to draw the text;if a button was required, an NSButtonCell would be used, and so on. Additionally, only the visiblerows would ever be drawn, saving a lot of time. This meant that a table with five columns and2,000 rows would not have to manage 10,000 subviews (plus headers!), but would instead have fivesubviews, each of which would “stamp” the content for its corresponding row into place using itscell. It also meant that the NSTableColumnView class was implemented in a data-agnostic fashion,as discussed in the Model-View-Controller section above: it could be initialized with any subclass ofNSCell and would use regular NSCell methods to set its content and draw it at a specified locationwithin the column’s view. Any additional handling required by a specific type of cell would behandled by the table view’s controller via a delegate method, which would pass the cell along withits row and column location before the cell would be drawn.The upside to this pattern is that it’s a very efficient way to work with large collections ofcontrols. The downside is that any custom control would need to follow this pattern, creating acell for itself that would do most of the work and a control class that would simply re-export thecell’s interface. It also makes it difficult to implement things like providing, since view that wouldspan multiple columns in a table, or have different sections of a table display data in a differentmanner from one another. This paradigm is still true for NSControl instances today, but at leasttable views have another option, as in OS X 10.7 it was made possible to build tables using aview for each cell, with no limitations as to the classes of those views. This essentially matchesthe behavior of the UITableView class on iOS, which uses a view for each of its cells. On OS X,however, a view-based table can still have multiple columns. In this book, you will use the newerview-based Table API whenever you need to interact with or define a table’s contents.ButtonsButtons are the simplest form of control within AppKit. A button is simply an item that performsan action when it is clicked and that changes its state whenever it captures a mouse-click untilthe mouse button is released. Typically buttons will darken slightly when clicked and will revert totheir original visual state when the mouse button is released. It performs this handling using theevent-tracking facilities of the NSControl class, telling its cell to become highlighted on mousedownand to unhighlight on mouse-up or when the mouse is moved outside the bounds of thecontrol. As noted, the control simply manages some higher-level abstract concepts while its celldetermines what it means to be highlighted, selected, bordered, and more.www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!