如何从派生类访问只读属性(在类继续中定义的读写)

时间:2013-10-09 12:09:05

标签: objective-c properties ios7 derived-class

我在以下情况中:

我已经定义了一个带有属性的类来访问我使其成为只读的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控制器。

2 个答案:

答案 0 :(得分:1)

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

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

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

这将允许我们在基础和孩子

中继续以下课程延续
@interface ChildViewController ()

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

@end

答案 1 :(得分:1)

最好不要公开公开实例变量,除非你真的需要。

使子类可以访问类的一些附加部分的标准模式是创建一个单独的头文件,例如, BaseViewController+Private声明为readwrite。然后,这个文件可以包含在'内部人'中,即类和它的子类。

相关问题