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.

2
  • are you initializing your myClass object? Commented Jun 23, 2012 at 8:34
  • I'm not sure but I guess not, I only did what I wrote. Commented Jun 23, 2012 at 8:44

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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?
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.
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.
Ok thats sounds complicated. If I instantiate the class in appDelegate, how do I access it from a vc? Thanks!
its basically this stackoverflow.com/questions/5082738/… you could even write a property or a method, its your choice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.