Importing code from different .m file

Discussion in 'iOS Programming' started by moonman239, Sep 27, 2013.

  1. moonman239 macrumors 68000

    Mar 27, 2009
    I have a view controller that is a subclass of a subclass of UIViewController. I subclassed a subclass because my app will have multiple view controllers that are not very different from each other code-wise. The problem is that I can't figure out how to make it so that all those view controllers inherit the code in the base class's .m file.
  2. chown33 macrumors 604

    Aug 9, 2009
    descending into the Maelström
    You're either overthinking this, or you don't understand how inheritance works in subclasses.

    Suppose you have a subclass. Call it Bulldog. You want it to subclass the Dog class. What do you write?

    First, you'd import the header for the Dog class. Write the code for that.

    Second, you'd declare the @interface of Bulldog so it inherits from the Dog class. Write the code for that.

    Third, you add whatever methods, variables, properties you want Bulldog to have that the Dog class doesn't. Write the code for that.

    Put all the code in the "Bulldog.h" file, because that's where you define the public interface of a new class.

    Now, and only now, should you start writing the implementation of the Bulldog class. Where does it go? In "Bulldog.m".

    At this point, ask yourself what the Bulldog class inherits from the Dog class? By default, and writing no new methods, it inherits every method, property, and instance variable already defined by the Dog class. It also inherits everything from Dog's superclass. And so on up the line to NSObject. Why? Because that's how inheritance works between subclasses and their superclasses.

    So the answer to your question is simple: If the class is declared as a subclass of something, it already inherits everything that class provides. If that doesn't make sense, then you need to review how inheritance works in classes.
  3. moonman239 thread starter macrumors 68000

    Mar 27, 2009
    Thank you. I have a question. Why would I want to put all the methods that the Bulldog class will have that the Dog class won't in Bulldog's interface rather than its implementation?
  4. chown33 macrumors 604

    Aug 9, 2009
    descending into the Maelström
    It should be obvious from the actual words: the @interface defines the interface of the class, and the @implementation defines the implementation of the class.

    If you don't understand the difference, then you really need to go back and study the fundamentals.

    Start with Cocoa Core Competencies, the Class definition heading, then read more articles and links starting from there. In particular, read the Definitive Discussion link.
  5. Duncan C macrumors 6502a

    Duncan C

    Jan 21, 2008
    Northern Virginia
    WHen you define your class hierarchy, you add methods to each class's .h file that will be available to other classes. The .h file, or interface file, defines the INTERFACE of the class.

    When you define a subclass of a class, the subclass does a #import on the header of it's superclass. That way it inherits the interface to all the methods defined in the header of the parent class.

    You can create methods that are only in the .m file and not in the .h file, and those will not be visible from other classes, or from subclasses. You do that for methods that are only intended for local use. It sounds like that's the mistake you are making. You are forgetting to include the definition of public methods in your header file, and so when you create a subclass, the subclass can't see those methods.

Share This Page