Search code examples
iosobjective-csubclassing

Can I subclass a custom view controller


I made a subclass of UITableViewController named CalendarTableViewController.h and .m. It shows a list of events within specified range (e.g. all events in 2014). I'd like to have two more view controllers (for month and day) and stack them on top of the first view controller.

Because all three view controllers have similar properties and behaviors, I'd like to subclass my custom view controller. Is this possible?

I'm about a year into developing with Xcode, so I may be missing something stupidly simple. But, all I know is how to subclass UITableViewController. How do you subclass your own custom view controller?

I would appreciate if you could share your wisdom. Yoshi


Solution

  • In the world of object-oriented programming, objects are categorized into hierarchical groups. Rather than using distinct terms for the different hierarchical levels such as genus or species, objects are simply organized into classes. In the same way that humans inherit certain characteristics as members of family, a class can be set to inherit functionality from a parent class.

    When one class inherits from another, the child inherits all the behavior and properties defined by the parent. It also has the opportunity either to define its own additional behavior and properties, or override the behavior of the parent.

    Long story short, yes, you can and you should create subclass(es) in such cases. This is how you do in Objective-c in .h file (in your new class)

      @interface MySecondController : CalendarTableViewController {
      }
    

    In Swift

      class MySecondController: CalendarTableViewController {
          // subclass definition goes here
      }
    

    I hope it helps