Question

I am in the following scenario:

I have define a class with a property to access the activeController that i made read-only

@interface BaseViewController : UIViewController

@property (nonatomic, weak, readonly) UIViewController *activeController;

@end

In the class continuation i have define the property as readwrite as i want to be able to set the active controller only within the class:

@interface BaseViewController ()

@property (nonatomic, weak, readwrite) UIViewController *activeController;

@end

How do i make the readwrite property accessible from a derived class?

@interface ChildViewController : BaseViewController 
@end

The compiler only see the property defined as read-only in the derived class and i want to be able to make use of property within my derived class and set the activeview controller in the derived class.

Was it helpful?

Solution

It's better not to expose publicly an instance variable unless you really need to.

The standard pattern for making some additional parts of the class accessible to subclasses is making a separate header file, e.g. BaseViewController+Private with the declaration of readwrite. This file can then be included by 'insiders', that is class and it's subclasses.

OTHER TIPS

You need to change the header file for BaseViewController to

@interface BaseViewController : UIViewController
{
    __weak UIViewController *_activeController;
}

@property (nonatomic, weak, readonly) UIViewController *activeController;

which will allow you to us the following class continuation in the both the base and the child

@interface ChildViewController ()

@property (nonatomic, weak, readwrite) UIViewController *activeController;

@end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top