Lizza
Lizza

Reputation: 2819

How can I share properties across custom and Apple-provided classes through inheritance?

I have a few different viewControllers that need to inherit the same properties, but aren't the same type of viewController. For example, one VC is a regular UIViewController, whereas another one is a UISplitViewController. Is there any way for me to efficiently use inheritance to make sure they all have these certain properties? Or do I just need to give each one their own separate declarations?

Upvotes: 1

Views: 88

Answers (4)

Cameron
Cameron

Reputation: 1142

Why not set up inheritance using a shared base class and set those shared properties in the init?

//MyBaseVC.h
@interface MyBaseVC : UIViewController
@property (nonatomic, strong) NSString *myString;
@end

//VC1.h
@interface VC1 : MyBaseVC
@end

//VC2.h
@interface VC2 : MyBaseVC
@end

-----

//(MyBaseVC.m)
-(id) init {
    self = [super init];
    if(self){
        self.myString = @"Hello world!";
    } 
    return self;
}

// VC1.m
-(id) init {
    self = [super init];
       NSLog(@"%@", self.myString); // "Hello world!"
    return self;
}

// VC2.m
-(id) init {
    self = [super init];
       NSLog(@"%@", self.myString); // "Hello world!"
    return self;
}

At that point, you can directly refer to the property on the subclassed objects:

NSLog(@"%@",myVc1.myString); //"Hello world!"

Otherwise, when you reference the VCs in a more generic fashion, you can always refer to their super class (MyBaseVC) - for example, if you need to pass them as a method parameter.

//-(void)doSomethingWithVC:(MyBaseVC *)vc;
 [someObj doSomethingWithVc: vc1];

Upvotes: 0

rmaddy
rmaddy

Reputation: 318934

You can achieve what you want using a category on UIViewController. You can implement the properties in the category using associated objects.

See Objective-C: Property / instance variable in category for more details.

Upvotes: 3

Scott Berrevoets
Scott Berrevoets

Reputation: 16946

You could add a category to UIViewController. Since UISplitViewController inherits from UIViewController, it will have all properties and methods as defined in the category as well. However, categories have two limitations:

  1. You can't add backing instance variables. You can create properties, but they can't have instance variables backing them. That means that if you are overriding the getter (and setter, if readwrite), so that it reads (or writes) an already existing property in some way, you're good. If not, you can look at associated objects.

  2. Overriding methods in a category is a no-no. While nothing stops you from doing it, you have undefined behavior if another category overrides that method too. You just don't know which method will get executed. If you need to override methods, subclassing UIViewController would be better. However, UISplitViewController will then not know about these properties, unless you subclass it as well and add those same properties (in which case you're maintaining these properties twice).

Upvotes: 2

Maciej Kozieł
Maciej Kozieł

Reputation: 997

I'm not sure what exactly do you need. If you don't want to (or can't) use common superclass with public properties, you can always write protocol. Only difference is that, protocol don't give you common implementation, but force you to write one (so you can be sure it is there, as you asked for).

Upvotes: 1

Related Questions