0

I have a customView. It has some condition like this(only example):

customView(viewsNeed: Bool)

...

if viewsNeeded {
    self.addSubView(newView)
    self.addSubView(newView2)
} else {
    self.addSubView(newView3)
    self.addSubView(newView4)
    self.addSubView(newView5)
}

and then I can add this View to in my ViewController:

self.view.addSubView(customView(viewsNeeded))

What I want to know is what should I do? Write conditions like this, or make separate Views for this purpose. Something like:

View1

...

self.addSubView(newView)
self.addSubView(newView2)

View2

...

self.addSubView(newView3)
self.addSubView(newView4)
self.addSubView(newView5)

And add one of them in the ViewController:

if viewsNeeded {
    self.view.addSubView(view1)
} else {
    self.view.addSubView(view2)
}

What kind of View creating is better in what situation, and how should i decide this kind of things? I need some really wide answers with explanations if it's real.

Tom Elliott
  • 1,908
  • 1
  • 19
  • 39
  • I prefer the second approach because it makes the views independent. In the first example, the `viewsNeeded` parameter makes `customView`'s behaviour linked to external state. (But I wouldn't call my preference "right" in any absolute sense.) – Phillip Mills Sep 25 '15 at 12:57
  • I think it depend on many thing. Example: if you have simple View with complex variable part, you can us 2 views, but if you have big complex View with some little different block, what you should do? and if you have both complex View and complex variable part of View, how to make design in this case to make your app better for perforce. – Zoltan Kurtyak Sep 25 '15 at 13:01

2 Answers2

1

If a view can have different states, you would take care of those different states within the view that has a certain responsibility. The UINavigationBar is a good example. It has a clear purpose, giving navigational context to the user, but it's state (and context) can make it appear different.

func pushNavigationItem(...) {
  ...
  if self.items.count > 1 {
     // show backButton
  } else {
     // hide backButton
  }
}

If the different views don't work together for a shared purpose, I wouldn't group them together in a container-view, but instead add them separately, dependent on your needs in a ViewController.

override func viewDidLoad() {
  if userDidBuyContent() {
    // add view with bought content
  } else {
    // add view to buy content
  }
}

And in general it's a good practice to keep your view-hierachy as flat as possible. The less views you introduce, the better your app will perform. The decision is ultimately up to you, but just keep in mind what the purpose of a view is and whether subviews contribute to that purpose or are really serving some other purpose.

Justin Hammenga
  • 1,091
  • 1
  • 8
  • 14
0

there is no conceptual difference between options you've described. from MVC pattern perspective they are both slightly wrong. you don't have to add views manually, view must create its structure itself.

heximal
  • 10,327
  • 5
  • 46
  • 69
  • Maybe its wrong, but i cant add UITapGestureRecognizer to my ImageView, but i can add it to UIView, so there is some situations when you need to add Views inside your View (ScrollView is better example), and there mast be some performance or principal difference in this options. – Zoltan Kurtyak Sep 25 '15 at 13:08
  • And why the second way is wrong if there you create you View inside itself, so it "create its structure itself", or am i getting something wrong? – Zoltan Kurtyak Sep 25 '15 at 13:13
  • yes, you're right, actually view creates itself in both cases. so mo matter at all from my perspective, you don't violate any known architect pattern) – heximal Sep 25 '15 at 13:23
  • but what is the better option for performance or just programming experience? I have not to much experience in programming, so i decided to ask about this from more experienced programmers. – Zoltan Kurtyak Sep 25 '15 at 13:25