dominic_89
dominic_89

Reputation: 59

Xcode : Accessing method instance from other class file

I've reasonable successfully setup access to another method in the following way:

SomeScript.m (the class I am trying to access)

-(void)amethod{....

codeA.h (the class that is accessing amethod) within the {} has:

SomeScript* myScript;

codeA.m

myScript = [[SomeScript alloc] init];
[myScript amethod];

What I want to do however is make it an instance variable of the app delegate but when I put the SomeScript* myScript; and the myScript = [[SomeScript alloc] init]; in AppDelegate.h the codeA.m does not recognise it.

Upvotes: 2

Views: 3353

Answers (1)

sch
sch

Reputation: 27506

First, add a property myScript to AppDelegate:

In AppDelegate.h

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) SomeScript *myScript; // Add this line

//...

@end

In AppDelegate.m

@implementation PCAppDelegate

@synthesize myScript; // Add this line

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.myScript = [[SomeScript alloc] init]; // Add this line

//...

@end

Now that you have declared and initialized the property, you can use it like the following from other classes:

AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
[appDelegate.myScript aMethod];

Upvotes: 2

Related Questions