Home > iPhone > Cocoa Custom Delegate

Cocoa Custom Delegate

Been spending times to port my current iPhone arabic library into iPad (Universal), and some of the codes I made was hardcoded, so there are some repeated codes here and there for SQlite3 query, since I want to overhaul my app, so I was thinking that I must use custom delegate for my DBManager.

On some tutorials on the net, people not using protocol for delegate, but I want it to be protocol based delegate.

So, here are the codes:

DBDelegate:

@protocol DBDelegate

@optional
- (void)dbPrepare:(NSString *)filename;
- (void)dbGotRecord:(NSString *)filename Statement:(sqlite3_stmt *)stmt;
- (void)dbFinalize;

- (void)dbGotBookInfoRecord:(NSString *)filename Statement:(sqlite3_stmt *)stmt;

@end

Then, on my DBManager:

@protocol DBDelegate;

@interface DBManager : NSObject {
id <DBDelegate> delegate;
}

@property (nonatomic, assign) id <DBDelegate> delegate;
@end

It used to be “id delegate”, but since we are making a protocol based delegate, we must make sure that our delegate object conforms to DBDelegate protocol, by using conformance statement of id, this way we don’t need to check if a delegate is conforms to DBDelegate or not.

On the codes where we fetch the records, we can call the delegate:

if (self.delegate)
{
[self.delegate dbGotBookInfoRecord:file Statement:stmt];
}

That’s it we now ready to use the delegate

Advertisements
Categories: iPhone
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: