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