0

I have a custom class which I want to "load" inside the firstViewController and then access it from other classes by segues. My Problem is, I can't even access and change the instance variable inside the firstViewController. Somehow I'm "loading" it wrong. Here is the code I used until now:

inside viewController.h

@property (strong, nonatomic) myClass *newClass;

inside viewController.m

@synthesize newClass;

I then try to access it by:

self.newClass.string = @"myString";
if(newClass.string == @"myString"){
  NSLog(@"didn't work");
}

Well, I get "didn't work". Why is that? When I write

myClass *newClass = [myClass new];

It does work. But the class and its properties gets overwritten every time the ViewController loads again.

What would you recommend? Thank you very much.

Linus
  • 4,643
  • 8
  • 49
  • 74

1 Answers1

2

Like Kaan said, you forgot to initialize your class, You have only declared and created a pointer for it but not the actual object, on your ViewDidLoad add

self.newClass = [[myClass alloc] init];

It does work. But the class and its properties gets overwritten every time the ViewController loads again.

That's because every time that specific Viewcontroller loads you are reinitializing the class.

If you want a persistent class through all your program look for the singleton pattern.

This is used in the case when you want to have only 1 instance of a certain object, if you try to initialize another instance of that object you will just receive the one you already have.

PD: newClass.string == @"myString" is wrong.

Use the isEqualToString method when comparing strings.

newacct
  • 119,665
  • 29
  • 163
  • 224
Pochi
  • 13,391
  • 3
  • 64
  • 104
  • Thank you for your answer! But I realized it maybe doesn't help me, even if it is a great solution. The problem is, I want to have 4 instances of this one class. And they all should be accessible from other viewControllers. Is this possible? – Linus Jun 23 '12 at 10:42
  • 1
    yes, you need 4 instances each will be different, so just create a simple class which contains instantiation of all these 4 and just pass this class around, be careful to only initialize this class once and to pass the class to all the places you need them. – Pochi Jun 23 '12 at 13:17
  • 1
    Or you could just pass each one separately, your only problem is that you are reinitializing the class when your viewcontroller is being loaded. Just instantiate all of those classes somewhere you know that will be kept alive. Even the app delegate works for this case (all your vc have access to it) its really just a matter of design. – Pochi Jun 23 '12 at 13:19
  • Ok thats sounds complicated. If I instantiate the class in appDelegate, how do I access it from a vc? Thanks! – Linus Jun 23 '12 at 13:26
  • 1
    its basically this http://stackoverflow.com/questions/5082738/ios-calling-app-delegate-method-from-viewcontroller you could even write a property or a method, its your choice. – Pochi Jun 23 '12 at 15:17