0

in my AppDelegate I have imported the header of a class I have created and propertied and syntesized an instance of it (in AppDelegate). Now I'm trying to access a method and a variable inside this instance from two other views. I'm relatively new to objective-c, but so far I've learned that if I do this:

AppDelegate *appdelegate = [AppDelegate new];

I will just get a fresh instance from the class inside AppDelegate, so if I set a variable from one view, I can't access it from the other. I've read that if I would do it this way:

AppDelegate *ap = (AppDelegate *)[[UIApplication sharedApplication] delegate];

It would allow me to access the existing instance. But it doesn't work.

Is the way I'm trying to do this totally wrong? Thanks a lot for your help!

UPDATE:

When I do this inside AppDelegate:

myClass.string = @"test";
NSLog(@"appDelegate: %@", myClass.string);

I get this:

appDelegate: (null)

UPDATE2:

I wrote @class AppDelegate; underneath the @import lines in the viewController, but still I can't access myClass. A main problem, which may be the cause why this isn't working from the views, is that I can't even access myClass from AppDelegate.

In AppDelegate.h I wrote:

@property (strong, nonatomic) testClass *myClass;

In AppDelegate.m:

#import "testClass.h"
@synthesize myClass;

This should be right, right?

myClass.h

 @property (strong, nonatomic) NSString *string;

myClass.m

@synthesize string;

When I then try to access myClass from appDelegate, I write:

self.myClass.string = @"test";
NSLog(@"appDelegate: %@", self.myClass.string);

The result is:

appDelegate: (null)
2
  • 2
    You should be able to get the sharedApplication delegate using the code in your second example, assuming you're programming on iOS. The delegate you set has to conform to the UIApplicationDelegate protocol. But "it doesn't work" is pretty broad. What doesn't work? Does it return nil? Or is it the calling of your method which doesn't work. Any errors? Any warnings? Commented Jun 21, 2012 at 12:55
  • Thanks so far. Yes, I'm programming on iOS. There is no error at all. The method from the instance doesn't seem to be called at all. What does that mean that it has to conform to the UIApplicationDelegate? Am I missing a code snipped? Commented Jun 21, 2012 at 13:10

3 Answers 3

2

I think you have to allocate and initialize the myClass

Write myClass = [MyClass alloc] init] in AppDelegate.m file

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

Comments

2

#import "AppDelegate"

and also write

@class AppDelegate;

Comments

2

Unless you haven't shown it, you're not allocating testClass and not assigning it to myClass. Objective-C is not like C++ or Java where you can simply declare a variable of a particular class type and have it instantiated on the stack. Each class you use must be instantiated, whether manually or through InterfaceBuilder. The exception is there are some classes provided by the various frameworks which have a single shared instance. Rather than allocating those classes, you simply ask for the shared instance. However, that's not the case here. It's your own class, so you need to allocate it.

It would look like:

myClass = [[testClass alloc] init];

Comments

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.