1

I have a question that why should we create a class derived from UIView when I can put as many UIViews in any view controller and can access them with IBOutlet.

Is there any special functionality which we get after subclassing UIView. May be its not a valid question, But I seriously wish to know the answer.

Any help will be appreciated.

Thanks In Advance. example:

@Interface CustomView: UIView
{

}
Kanan Vora
  • 2,124
  • 1
  • 16
  • 26
Sundeep Saluja
  • 1,089
  • 2
  • 14
  • 36

5 Answers5

2

Mostly you subclass UIView or any UIComponent for customization. When you subclass you can override drawRect method and do your own customization.

See the best practices for subclassing UIView here.

Community
  • 1
  • 1
Vignesh
  • 10,205
  • 2
  • 35
  • 73
  • You can google it. A sample from quick search http://www.fruitstandsoftware.com/blog/2009/12/custom-drawing-using-drawrect-part-1/ – Vignesh Mar 18 '14 at 07:40
1

It simply depends! What if I want to draw some custom shape on my view? I can just subclass a UIView and use drawRect: make whatever I want and then use it. So, the reasons are numerous but really depends on your desire.

To support further. Refer the below link:

https://softwareengineering.stackexchange.com/questions/209576/what-are-the-reasons-to-create-uiview-subclass

Community
  • 1
  • 1
GenieWanted
  • 4,473
  • 4
  • 24
  • 35
1

Subclass is required only when you need to add some extra properties, behaviors, like the view should have some glossy effect, round rect corner etc.

However from UIView or from any other Cocoa Classes (foundation or UIKit or any other kit) they give you most of the required-common features, but at times as per your requirement you need to put something extra. For this purpose you extent the class by

  1. Subclassing or Inheritance.

  2. Class Extension.

  3. Categories.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
1

Custom views is easier to reuse. You can incapsulate some logic inside view (drawing logic for example) and then reuse this view.

In my current projects book pages are shown in several places. So it is very convenient to incapsulate page-presentation/drawing in one UIView.

Avt
  • 16,927
  • 4
  • 52
  • 72
1

There are several use cases that can be applied to UIView inheritance, such as:

  1. implementing a custom control
  2. extending UIView with methods that implement specific functionalities (although it can also be done by using a category, although that works in a different way and usually applies to different use cases)
  3. group correlated views into a single view, to improve usage, expose a clean interface and deal with views management internally
  4. create a base view that you want to reuse in other UIView inherited classes, without having to implement the custom look and/or behavior on each subclass

Below some examples

Custom control - You want to implement a 4 states flat push button, which 'advances' to the next state on every tap. Each state is visually represented in a different way, for instance by changing the label, the border, and/or the color. Note: in this example it would be better to extend the UIControl or UIButton, which are both subclasses of UIView

Extension - You want a view that has a rounded border, a title and a body section, with the title having a bigger font and on inverted colors (white text on dark gray background).

Grouping - You have a panel where you want to display a user profile, with several labels and values (i.e. User Name: myusername), a profile picture, dynamically laid out.

Base Class - you have a set of views that share a common look, with a rounded border, a title at top right overlapping the border, and an internal panel where to lay out content. You create a UIView subclass called SOMyPanelBaseView, and then inherit other classes from it, automatically inheriting the look

In many cases (but not all) you can skip inheritance and implement everything in a view controller, but besides being a not good object oriented design (the view is highly tied to the view controller), it makes the view controller over complicated. By inheriting from a UIView instead you implement the view functionalities in a separate class, and plug those functionalities into the view controller. Also, an UIView inherited class can be reused - whereas if you implement everything in the view controller, you can't unless you copy and paste code around.

To sum up, I think your question can be generalized as: why should I use inheritance in object oriented programming? The same reasons behind using inheritance in OOP is the same as using it in a specific case, in our case for UIView. The topic is described pretty well on wikipedia

Antonio
  • 71,651
  • 11
  • 148
  • 165