我确实有一个小问题。 由于我的 UIViewControlles 都以相同的方案命名:
MyView1Controller.h MyView1Controller.m 我的View1.xib
MyView2Controller.h MyView2Controller.m MyView2.xib
MyView3Controller.h MyView3Controller.m MyView3.xib
我现在更愿意通过工厂方法初始化我的 UIViewControllers。 因此我会在 UIViewController 上实现一个 Cateogry:
static NSString *standardNibFileName;
@interface UIViewController (FactoryInstantiation)
+ (id) standardViewController;
@end
在 MyView1Controller Controller 中,我将声明静态 nib 文件名变量:
static NSString *standardNibFileName = @"MyView1";
@implementation MyView1Controller
然后我可以使用以下方法实例化我所有的 UIViewCONtroller:
@implementation UIViewController (FactoryInstantiation)
+ (id) standardViewController;
{
if(standardNibFileName != nil) {
NSString *className = NSStringFromClass([self class]);
Class classToIntantiate = NSClassFromString(className);
return [[classToIntantiate alloc] initWithNibName:className bundle:nil];
}
return nil;
}
@end
初始化:
MyView1Controller *a = [MyView1Controller standardViewController];
但是静态变量总是nil。
关于如何解决这个问题有什么建议吗?
如有任何帮助,我将不胜感激!
提前致谢。
最佳答案
您可以在 UIViewController
类上声明一个 + 方法并覆盖实现类
+ (NSString*) getStandardNibFileName {
return @"nibName"
}
编辑:如果实现类与基类具有相同的 nibName,则您不必覆盖该函数。
关于iphone - Objective C - 类别中的静态变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8604958/