Reputation:
I have a view controller without a nib. I'm creating the view in code. I have been doing it like this in viewDidLoad
UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 475, 50.0)];
self.headerView = header;
self.headerView.backgroundColor = [UIColor yellowColor];
[self.view addSubview:self.headerView];
UIView *footer = [[UIView alloc] initWithFrame:CGRectMake(0, self.view.height - 50.0, 475, 50.0)];
self.footerView = footer;
self.footerView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin;
self.footerView.backgroundColor = [UIColor greenColor];
[self.view addSubview:self.footerView];
and so on. However, as my viewDidLoad
becomes longer and longer I get the feeling that this maybe shouldn't be in my Controller. Should this really be a subclassed UIView that is then loaded in my viewDidLoad
? What is the MVC best practice for this kind of situation?
Upvotes: 1
Views: 168
Reputation: 1522
Controller is usually the "boss" - it brings together model and view. So it can have intimate knowledge of both layers. From this perpecive having it create view hierarchy isn't necessarily bad, although as @danh noted, you should decide on case per case basis. If the code starts "smelling" than go ahead and refactor it. If your view hierarchy starts to get really complex, then you may need to introduce additional controllers that would divide responsiblites. For example you may have controller for handling header, another for handling footer etc.
Upvotes: 0
Reputation: 14662
If you feel that your header and footer could be reusable, then yes I would make new classes for those. Reusable here doesn't necessary means that you will be reusing these somewhere else in your application, just that could be eventually be done.
If that's not the case, you could simply add a method createHeader
that you would call from viewDidLoad
.
Upvotes: 1
Reputation: 86651
Should this really be a subclassed UIView that is then loaded in my viewDidLoad?
In my opinion: yes. I would probably have a class for the controller's view and that would create the header and footer views (which could themselves be subclassed from UIView).
What is the MVC best practice for this kind of situation?
These views and subviews are all part of the "V" part of MVC. The more I think about it, the more I think that, as you have things at the moment, the controller is doing some of the "V" work. I'm not saying that is necessarily bad but you should certainly think about better separation as your view creation code gets bigger.
Upvotes: 1