I have several viewControllers in my application that I want to inherit from a single parent viewController which of course, extends UIViewController (thus making all of the viewController classes with the exception of the parent, sibling classes). What I would like to know (and I am new to inheritance in Objective-C), is that in my parent viewController class, I am declaring a parameter in the .h file called, NSString *name, which I will be marking as a @property (nonatomic, retain), and then synthesizing in my .m file.
After doing this in the parent class, do I have access to the NSString *name parameter in the child viewControllers, or do I have to declare a separate parameter in each respective viewController class that inherits from the parent class? Each viewController will have a unique value for the NSString *name parameter, and if this is the case, do I need to create a separate parameter for each viewController class, or would it be sufficient for me to simply create it once inside the parent class?
I also want to confirm that if I declare any methods in the parent class, that I would be able to access them using a reference from the sub-classes (so long as that method is not overridden in the child class), as I am able to in Java? My guess is yes, since this is one of the essential principles of Objective-C.
If each view controller has a unique value, and that value is immutable, then you can simply override the getter for your
nameproperty and return a value unique to that view controller:If you would like to be able to mutate the result, you can declare either an iVar, or a property with a different name, then return that in a getter for the class:
Also, to answer your second question, Variables, Methods, and even Properties declared inside the method file of a class are invisible to subclasses, where methods and properties declared in the header are visible to all that import the header. iVars can always be accessed through the struct access operator (
->), but unless they are marked@public, the compiler will discourage you from going this route.