I have a class which implements many delegate methods. How to group the delegate methods into different classes by the protocol they belongs to and use them in the original class?
+2
A:
Rather than creating many classes, a simpler solution is to divide the class into different categories:
@interface MyViewController : UIViewController {
...
}
...
@end
@interface MyViewController (TableStuff) <UITableViewDataSource, UITableViewDelegate>
// methods related to table stuff
@end
@interface MyViewController (SearchStuff) <UISearchBarDelegate>
// methods related to table stuff
@end
Since categories just add methods to the existing class, you could use the any methods declared in a category in the "original" class.
KennyTM
2010-09-06 20:13:51
Thank you! Is it kinda best practice for this solution? Do I really need to separate them in practice?
sza
2010-09-06 20:15:38
@sza: No. It just makes it easier to organize. You could put all delegate methods into the same class without any categories.
KennyTM
2010-09-06 20:26:40