16

Here are my objective-c classes:

AppDelegate
SomeScript

How might I call the function loggedIn on the SomeScript class from the app-delegate or any other class?

4 Answers 4

45

(I'll assume loggedIn is an instance method taking no parameters.) First, several terminology issues:

  1. They're not functions, they're methods (same idea, though).
  2. You don't call methods, you send messages (usually same idea, though).
  3. Most importantly, we usually send messages not to classes, but to instances of those classes. (If you can't visualize the difference, imagine placing a letter in the idea of mailboxes vs. placing a letter in your mailbox. Only one makes sense!)

So, our new plan is to first instantiate SomeScript, then send a message to the instance.

SomeScript* myScript = [[SomeScript alloc] init]; //First, we create an instance of SomeScript
[myScript loggedIn]; //Next, we send the loggedIn message to our new instance

This is good. However! I bet you want your script to stick around for later use. Thus, we should really make it an instance variable of your app delegate. So, instead, in AppDelegate.h, add this inside the braces:

SomeScript* myScript;

Now our variable will stick around, and our first line from before becomes simply:

myScript = [[SomeScript alloc] init];

Last complication: we don't want to create a new script every time we call loggedIn (I assume)! So, you should place the instantiation somewhere it will only be run once (for example, application:DidFinishLaunchingWithOptions:). Ta-da!

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

5 Comments

You need to add something on releasing myScript
@JeremyP: It's in his AppDelegate. @ChristianStewart: Jeremy's right, you should usually send release to myScript when you're done with it. I didn't tell you because I assumed you would be using it the entire time your app is open.
It doesn't do any harm to get into good habits. Technically, you don't need to release anything your app delegate holds on to because the app delegate itself stays around until the end. However, it's a good idea to get into the habit of putting the release in -dealloc. It needs to become automatic.
Yes, I ended up doing this anyway Jeremy :D
Not anymore with auto reference counting!
4

You shall have an initialized reference of a SomeScript object in your AppDelegate class (supposing you do not need SomeScript to be a Singleton class like your AppDelegate). Something like:

SomeScript * myScript;

as an ivar in your AppDelegate interface, while in its application:DidFinishLaunchingWithOptions:

you have inited it (let's suppose with the default alloc/init combo calling):

myScript = [[SomeScript alloc] init]

Done all of this, when you need to call a method of myScript you can simply do:

[myScript myMethod:myParameter]

Here you can find a nice guide for beginners from Apple

Comments

3

If you don't want to use instances of SomeScript ... you can follow a different approach. Use NSNotificationCenter for sending a notification to your SomeScript object and make it run a selector after that.

In your -(void)awakeFromNib{} method, from SomeScript place the following code :

  [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(mySelector:)
                                                 name:@"aUniqueNameForTheNotification"
                                               object:nil];

Create the method "mySelector:" and place the the call to your loggedIn method. (Or if you prefer, you could replace "mySelector:" with loggedIn directly)

-(void) mySelector:(id)elem
{
    [self loggedIn];
}

Then don't forget to remove the observer on dealloc, so place the following piece of code in your SomeScript class also :

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

Then you can send a notification from any other like so :

 [[NSNotificationCenter defaultCenter] postNotificationName:@"aUniqueNameForTheNotification"  object:self];

That last piece of code sends a notification to SomeScript and your selector is executed.

Hope it helps you guys!

Comments

2

We can call it like [self loggedIn]
When loggedIn method is in SomeScript class, using simple syntaxes in latest xcode.

[[SomeScript new] loggedIn];

1 Comment

with ARC no deallocation required.

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.