Alexander
Alexander

Reputation: 7238

Objective C - static variables in categories

I do have a little Problem. Since my UIViewControlles are all named in the same scheme:

MyView1Controller.h MyView1Controller.m MyView1.xib

MyView2Controller.h MyView2Controller.m MyView2.xib

MyView3Controller.h MyView3Controller.m MyView3.xib

I would now prefer to init my UIViewControllers via a factory method. Therefore I would implement a Cateogry on UIViewController:

static NSString *standardNibFileName;

@interface UIViewController (FactoryInstantiation)

+ (id) standardViewController;

@end

And in MyView1Controller controller I would declare the static nib file name variable:

static NSString *standardNibFileName = @"MyView1";

@implementation MyView1Controller

Then I could instantiate all 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 the static variable is always nil.

Any suggestions on how to solve this issue?

I would appreciate any help!

Thanks in advance.

Upvotes: 4

Views: 1116

Answers (2)

Andreas Helgegren
Andreas Helgegren

Reputation: 1668

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

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

Edit: If the implementing class has the same nibName as the base you don't have to override the function.

Upvotes: 1

Nakkeeran
Nakkeeran

Reputation: 15296

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

Upvotes: 0

Related Questions