Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Objective-C.Programming.pdf
Скачиваний:
14
Добавлен:
21.02.2016
Размер:
8.64 Mб
Скачать

Chapter 28 Your First Cocoa Application

BNRDocument instance, which is why we wired its action. The BNRDocument, however, does not need to send messages to the button, and so it does not need a pointer to it.

Revisiting MVC

Now that you’ve laid out your user interface, let’s have a look at the object diagram for this project:

Figure 28.15 Object diagram for TahDoodle

NSDocument, the superclass from which your BNRDocument class inherits, is an interesting beast. At first glance, it appears to be a model object. If you look up the class reference for NSDocument, though, you’ll discover that it’s more of a controller than anything else. NSDocument coordinates various disk-related activities and connects directly to the views responsible for issuing user input. When

you create the NSDocument subclass BNRDocument, you added pointers to the real model objects (an

NSMutableArray of NSString objects).

Edit BNRDocument.m

Now that you’ve got the user interface of your application created, configured, and connected, it’s time to get back to writing code. Click on BNRDocument.m in the project navigator to reopen it in the editor, and implement createNewItem:.

#import "BNRDocument.h"

@implementation BNRDocument

#pragma mark - NSDocument Overrides

- (NSString *)windowNibName

{

return @"BNRDocument";

}

202

Edit BNRDocument.m

#pragma mark - Actions

- (IBAction)createNewItem:(id)sender

{

// If there's no array yet, go ahead and create one to store our new task if (!todoItems) {

todoItems = [NSMutableArray array];

}

[todoItems addObject:@"New Item"];

//-reloadData tells the table view to refresh and ask its dataSource

//(which happens to be this BNRDocument object in this case)

//for new data to display

[itemTableView reloadData];

//-updateChangeCount: tells the application whether or not the document

//has unsaved changes. NSChangeDone flags the document as unsaved. [self updateChangeCount:NSChangeDone];

}

Now implement the required table view data source methods (as defined by the

NSTableViewDataSource protocol):

#pragma mark Data Source Methods

- (NSInteger)numberOfRowsInTableView:(NSTableView *)tv

{

//This table view is meant to display the todoItems,

//so the number of entries in the table view will be the same

//as the number of objects in the array.

return [todoItems count];

}

- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn

row:(NSInteger)row

{

//Return the item from todoItems that corresponds to the cell

//that the table view wants to display

return [todoItems objectAtIndex:row];

}

- (void)tableView:(NSTableView *)tableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn

row:(NSInteger)row

{

//When the user changes a to-do item on the table view,

//update the todoItems array

[todoItems replaceObjectAtIndex:row withObject:object]; // And then flag the document as having unsaved changes. [self updateChangeCount:NSChangeDone];

}

Build and run the program. TahDoodle will appear on the screen, and you can add and change todo items. The big missing feature, however, is the ability to save and reopen a to-do list. To make this happen, you need to override the following methods inherited from BNRDocument’s superclass,

NSDocument:

203

)/"" ( + %

2A "% N ;? 2AA "% ? 2

2AV ""% V

&

. # # ! # ! #

E I # # N6' ; ! #

# /

( "1 ( ! " "

@ # 7

# ) RN6%! L " "S

8

/ ! # " N6' ;

N6' # ) RN6 "-6 D

# E "-U #

UN6 "-F%-? G *G&

U&

U ! 5 S

! ! " / # N6' ;

! #

.

BNND% @ 2A "%

;? 2AA "% ? 2

2AV ""% V

&

. # # # ! # #

E # # N6' ; # I # ! ! # !

5I ! #

# ) RN6 "-6 D

"-E ' U#

UN6 "-%! 2

UNH--

U ! 5 S

! ! ! # # !

! # @)

.

& & ' )G ' ' ' * N65! + ' *N65

"-E ' U U U U) ) ) * + N65+ ) & & ) !

) ) ! + ) " * !

&& '

*- ) !

C * )N65 & " )' +N65 * + " & ' & !

)4!

Part V

Advanced Objective-C

You now know enough Objective-C to get started with iOS or Cocoa programming. But don’t rush off just yet. These next chapters provide a gentle discussion of techniques and concepts that will be useful in your first year as an Objective-C programmer.

This page intentionally left blank

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]