19

I developed some application 'till now. Now I'm writing a new one and in this project I want to keep the code very clean, so it's very easy to find the methods.
I want to start with the UIViewControllers whose view have a UITableView as subview. I wish to have a file with the name DetailViewController for all functions which belong directly to it. Another file with the name DetailViewController+Protocols should contain a category of the class above and all these delegate methods of UITableView.

Is it possible to do something like this? I want to keep my code clean and split it into multiple files.


EDIT

DetailViewController.h

@interface DetailViewController : UIViewController

... Some Properties

... Some Methods

@end

DetailViewController.m

#import "DetailViewController.h"

@implementation DetailViewController

... Some Synthesizes

... Some Methods

@end

DetailViewController+Protocols.h

@interface DetailViewController (Protocols) <UITableViewDelegate, UITableViewDataSource>

@end

DetailViewController+Protocols.m

@implementation DetailViewController (Protocols)

- (NSINteger)numberOfSections
{
    return ...;
}

- (NSInteger)numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
        return ...;

    return ...;
}

...

@end

But then Xcode shows a warning that some of the delegate methods are not implemented in DetailViewController. I also tried it with importing the DetailViewController+Protocols.h in the DetailViewController.h. No changing.
Then I tried it with ignoring the warnings and see: It worked! But why? Shouldn't it work without these warnings?

2 Answers 2

8

You don't have to create an entirely separate named protocol in a different file if you're only using it against one class. In fact, you probably shouldn't because it's just confusing.

If you want to keep your primary class' .h file clean, just move this:

@interface DetailViewController (Protocols) <UITableViewDelegate, UITableViewDataSource>

@end

into your primary .m file above the @implementation

I don't see why you wouldn't want to declare in your .h that your primary class implements the tableview delegate and datasource protocols, though. They're descriptive.

2
  • It's a way how to avoid burden viewcontrollers and keep them very light. Viewcontroller are usually the biggest files in MVC architecture which is wrong by definition. You should keep your classes as small as possible because of reusability and lucidity. I completely do not agree with that statement it's confusing. It's exactly the opposite - very clean and anyone familiar with this technique will benefit a lot from having delegates separated. By the way Swift is encouraging you to do it with extensions. Jan 20, 2016 at 20:22
  • Agreed -- mostly. In the special case of these delegates/datasources, you're either going to have a tight coupling (1:1) between the ViewController and the datasource, or you're going to reuse the datasourcefor multiple View Controllers, in which case you end up with a lot of conditionals/switches to handle each table argument that's passed in for each function call. In THAT case it makes sense to put it in a separate file to show that it serves multiple classes, but if a support class is only going to handle one kind of view controller, putting both in same file implies that coupling. Jan 22, 2016 at 5:18
5

Yes it is. The only thing to keep in mind with categories is that you cannot @synthesize properties in them. Adding ivars in categories is more difficult as well.

See this for more info on that.

2
  • 1
    I'm not getting compile warnings when I setup a test project doing this. Make sure that DetailViewController+Protocols.m imports DetailViewController+Protocols.h. Also that DetailViewController.m imports DetailViewController+Protocols.h. Nov 4, 2011 at 20:56
  • from this scenario inside the +props class, i have included a DetailViewController_private which contains a private interface with all my local props, i include this at the top of +props, but i cannot get access to self.label for example. Which step am i missing, anything obvious? Sep 13, 2013 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.