views:

26

answers:

1

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
Thank you! Is it kinda best practice for this solution? Do I really need to separate them in practice?
sza
@sza: No. It just makes it easier to organize. You could put all delegate methods into the same class without any categories.
KennyTM