考虑以下几个类的实现:
.h文件实现了三个不同的类
//superClass.h
...
@interface superClass
//Insert useful code
@end
//subClass.h
...
@interface subClass : superClass
//Insert useful code
@end
//Another class that uses various sub-classes of superClass
@interface anotherClass
{
superClass* myObj;
}
@property (strong, atomic) superClass* myObj;
@end
.m文件实施anotherClass
@implementation anotherClass
@synthesize myObj;
...
//All of the code that uses the myObj property...
...
@end
如何将subClass
的实例设置为属性anotherClass.myObj
的实例?
在我的具体情况下,我想定义一个UIViewController
的属性。我有多个子类,UIViewControllers,我需要能够在我的iOS应用程序中的特定属性上交换它们。
每次我尝试将我的一个孩子UIViewControllers设置为我的UIViewController
属性时,应用程序崩溃了。但是,如果我将属性重新定义为子类之一的实例然后实例化一个子类,它就可以完美地工作。我真的需要能够进出这些控制器。我怎么能这样做?
更新
好吧,向所有看过这个的人道歉。得到一个明显的答案后,我决定重新编写所有相关的代码。我创建了一个具有不同名称的新属性,注释掉了我的所有赋值代码并重新编写它以指向新属性。它的工作正常。我不确定为什么这不会第一次起作用,但它现在表现得应该如此。但是,我仍然不清楚为什么我的应用程序崩溃了。