iphone - Objective C - 类别中的静态变量

标签 iphone objective-c properties ios5 categories

我确实有一个小问题。 由于我的 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/

相关文章:

ios - 根据uiswitch状态显示数据

iphone - 在 Android 和 iPhone 上使用默认起始位置从浏览器打开谷歌地图应用程序

iphone - 从文本文件中读取 NSPoint

iphone - 优化快速重复调用的技巧(Objective-C,iPhone)?

ios - 如何将十六进制值的 NSString 转换为 Char - Objective C

java - 从一个位置共享属性文件

iphone - 打开并操作 .xcdatamodeld-File/SQLite 的记录?

ios - 在不同机器上运行 Sonar 扫描仪和 Sonar 服务器时出错

ios - 如何引用类的方法

maven - 如何设置从 Maven 中的依赖项引用的属性