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?
Thanks, Christian Stewart
(I'll assume loggedIn
is an instance method taking no parameters.) First, several terminology issues:
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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With