gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 19:48:18 25 4
gpt4 key购买 nike

我确实有一个小问题。由于我的 UIViewControlles 都以相同的方案命名:

MyView1Controller.hMyView1Controller.m我的View1.xib

MyView2Controller.hMyView2Controller.mMyView2.xib

MyView3Controller.hMyView3Controller.mMyView3.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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com