How to access a read-only propertly (re-defined readwrite in class continuation) from a derived class

StackOverflow https://stackoverflow.com/questions/19271596

문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top