Objective c 如何从派生类访问只读属性(在类继续中重新定义readwrite)

Objective c 如何从派生类访问只读属性(在类继续中重新定义readwrite),objective-c,properties,ios7,derived-class,Objective C,Properties,Ios7,Derived Class,我的情况如下: 我定义了一个具有属性的类来访问我设置为只读的activeController @interface BaseViewController : UIViewController @property (nonatomic, weak, readonly) UIViewController *activeController; @end 在类延续中,我将属性定义为readwrite,因为我希望能够仅在类内设置活动控制器: @interface BaseViewController

我的情况如下:

我定义了一个具有属性的类来访问我设置为只读的activeController

@interface BaseViewController : UIViewController

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

@end
在类延续中,我将属性定义为readwrite,因为我希望能够仅在类内设置活动控制器:

@interface BaseViewController ()

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

@end
如何从派生类访问
readwrite
属性

@interface ChildViewController : BaseViewController 
@end

编译器只在派生类中看到定义为只读的属性,我希望能够使用派生类中的属性并在派生类中设置activeview控制器。

您需要将BaseViewController的头文件更改为

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

@property (nonatomic, weak, readonly) UIViewController *activeController;
这将允许您在基类和子类中为我们提供以下类的延续

@interface ChildViewController ()

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

@end

除非确实需要,否则最好不要公开实例变量


使子类可以访问类的某些附加部分的标准模式是创建一个单独的头文件,例如带有
readwrite
声明的
BaseViewController+Private
。然后,该文件可以由“内部人员”包括,即类及其子类

我需要在基类和派生类中都使用activecontroller属性-我应该在基类和派生类继续中同时定义读写属性吗?