Goal C - Static Variables in Categories

I have a little problem. Since my UIViewControlles are named in the same way:

MyView1Controller.h MyView1Controller.m MyView1.xib

MyView2Controller.h MyView2Controller.m MyView2.xib

MyView3Controller.h MyView3Controller.m MyView3.xib

Now, I would prefer to initialize my UIViewControllers using the factory method. Therefore, I would execute Cateogry on a UIViewController:

static NSString *standardNibFileName; @interface UIViewController (FactoryInstantiation) + (id) standardViewController; @end 

And in MyView1Controller, I would declare a static variable for the nib file name:

 static NSString *standardNibFileName = @"MyView1"; @implementation MyView1Controller 

Then I could instantiate all of my UIViewCOntrollers using the method:

 @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 

Init:

 MyView1Controller *a = [MyView1Controller standardViewController]; 

But a static variable is always zero.

Any suggestions for resolving this issue?

I would be grateful for any help!

Thanks in advance.

+4
source share
2 answers

You can declare a + method instead of the UIViewController class and override it on the implementing classes

 + (NSString*) getStandardNibFileName { return @"nibName" } 

Edit: if the implementation class has the same nibName as the base, you do not need to override the function.

+1
source

You have a static NSString * standardNibFileName; in the .h file, try deleting it, I hope static NSString * standardNibFileName = @ "MyView1";. m is more than enough

0
source

Source: https://habr.com/ru/post/1387570/


All Articles