2

If I have a complicated view hierarchy in a UIViewController, when would it be appropriate to factor out the main view into its own class, even though it's not re-usable elsewhere? And if I were to do that, what would the proper event handling approach be for a button on that view - addTarget directly to a button property or delegation through the view class?

I'm having a lengthy debate with a colleague about whether we should always create a separate view class.

(For the purposes of this discussion, let's make the assumption that we want to avoid NIB files at all costs.)

xtravar
  • 1,321
  • 11
  • 24
  • Personally, I like my classes small and tidy. For that reason, as soon as I have something that can be seen as a separate "control", I create an UIView subclass for it. In the project I'm working on now, there's a bar of icons. I created a separate classes for the icon and the bar of icons. The reason is that I wanted to avoid mixing icon code with the bar code. Looking from that perspective, delegation looks more appropriate. That would also help in case you wanted to refactor the "control" later by, say, replacing UIButton with some custom implementation of a button. – Baglan Jan 29 '13 at 17:27

2 Answers2

0

You can create separate views for one view controller. If you want to load a particular view based on certain conditions, then you can have one custom init method to load the view to the view controller as given

- (id)initWithView:(UIview *)view {

self = [super init];

if(self) {

    [self setView:view];
}

return self;

}

And if you have different buttons in views, you can write button action methods in that views itself. For getting those actions to the viewcontroller you can write a protocol in views and set viewcontroller instance to delegate and implement those protocol methods in view controller. For differentiating the action, you can set tags for each buttons and according to that you can execute appropriate action in view controller.

Augustine P A
  • 5,008
  • 3
  • 35
  • 39
0

MVC should always keep controller small and clean. I ask my teams to always logically separate complex view into small views.

As for adding control, always try to simpler way. Use delegate when it is necessary.

SnowWolf
  • 439
  • 6
  • 22