Database Reference
In-Depth Information
Adding Our Code Files
Now that we have our storyboard set up it is time to write the code that will control it. Let's start
by creating a new Objective-C class that is a subclass of UICollectionViewController . Call it
FriendsCollectionViewController .
We also need to create an Objective-C class for the Collection View Cell. Add another Objective-C
class that is a subclass of UICollectiionViewCell . Let's call this one EntryCollectionViewCell .
Finally, we need to create another class for our UIViewController that will act as a detail
view. Add another Objective-C class that is a subclass of UIViewController and name it
FriendDetailViewController .
CTAppDelegate
In the CTAppDelegate.m file you need to delete two lines in the application:didFinishLaunchingWith
Options : method. Here are the two lines you need to remove:
MainViewController *controller = (MainViewController *)self.window.rootViewController;
controller.managedObjectContext = self.managedObjectContext;
EntryCollectionViewCell
Let's open up the EntryCollectionViewCell .h file and start writing our interface. We first want to add
a protocol definition just above the @interface line for our delegate protocol:
@protocol EntryCollectionViewCellDelegate;
Now add a method definition to configure the cell. Call this method configureCellForEntry:withDel
egate and it takes an NSString and a EntryCollectionViewCellDelegate as its parameters:
-(void)configureCellForEntry:(NSString *)entry withDelegate:(id<EntryCollectionViewCellDelegate>)
delegate;
The final piece of code we want to add is our full protocol definition. We add a delegate method
called entryCollectionViewCell:longPressedForEntry and it has an EntryCollectionViewCell and
an NSString as its parameters.
@protocol EntryCollectionViewCellDelegate <NSObject>
-(void)entryCollectionViewCell:(EntryCollectionViewCell *)cell longPressedForEntry:(NSString *)
entry;
@end
You may be wondering why we define the protocol in two places rather than just using the
preceding code by itself. Well, the reason is that we want to send back a reference to the cell and
at the first protocol definition we don't know anything about EntryCollectionViewCell because
the interface hasn't been defined yet. By adding the empty protocol we can reference the protocol
inside the interface. Then when we add the protocol definition again we can reference the
EntryCollectionViewCell object.
 
Search WWH ::




Custom Search