Calling methods in different classes - class

This question is semi-similar to the one asked here
But the difference is that I'm using cocos2d.
I have a method named screenshot in my AppDelegate. It's obviously used to take a screenshot. I want to call this method in another class, but simply doing [self screenshot]; isn't working out because I get the warning 'GameOver' my not respond to '-screenshot'.
All I want is for the screenshot method to be called in GameOver.
Thanks!

You can't call a method that does not exists - it will generate a runtime error.
If you want to call a method of AppDelegate from GameOver you should have a pointer to AppDelegate object if the method is not static:
[appDelegatePointer screenshot];
If the method is static
[AppDelegate screenshot];
I suppose you have only one object AppDelegate and you can make a static method
+(AppDelegate) sharedDelegate;
of class AppDelegate that will return your object singleton. And so you will be able to call screenshot method from GameOver object or from any other place like this:
[[AppDelegate sharedDelegate] screenshot];
Such thing is done in CCDirector, CCTextureCache, SimpleAudioEngine, ... classes in Cocos2D

Related

How to avoid multiple allocating of CLLocationManager instance?

I am using core location framework inside my app and sending updated location to server in didUpdateToLocation method and the view where i am allocating the CLLocationManager instance is the second view after login and this view is called from login view and releasing CLLocationManager in dealloc method but every time i am coming from login screen then didUpdateToLocation method calling twice and thrice depending on how many times i am coming from login view so whats the problem is and how can i avoid this?
If you want to avoid multiple allocations of your CLLocationManager instance, define the instance as a property of your appDelegate
Each time you need to get this instance you will do
YouAppDelegate *appDelegate = (YouAppDelegate *)[UIApplication sharedApplication].delegate;
appDelegate.yourLocationInstance;
I usually just use a singleton for this type of thing. Just define a class method (using a + vs a - on the method definition). Here's an example of a singleton definition:
+(id)sharedLocationManager
{
static BCLocationManager *sharedLocationManager;
#synchronized(self)
{
if (!sharedLocationManager)
{
sharedLocationManager = [[BCLocationManager alloc] init];
}
return sharedLocationManager;
}
return sharedLocationManager;
}
Basically you define a static instance of your class and only initialize it if it doesn't exist. With a singleton, you never access the instance init function. Always assign the result of the singleton accessor to the appropriate pointer like so:
BCLocationManager * testInstance = [BCLocationManager sharedLocationManager];
Now all you have to do is include the appropriate header.h and everyone in your application can see that single instance of your location manager. As long as you don't use an instance of init, you'll never create more than one.

iOS - Passing Values from an Object to it's Containing Class

The setup:
I have an AppDelegate and declared on that is CustomUIViewController.h and in that controller I have RandomName.h declared (as an object, not a subclass) can I use [super methodName] or similar to trigger a method on CustomUIViewController from a method on RandomName.h?
Or do I have to pass it to the appDelegate and then from there to CustomUIViewController? (How I have been doing it)
Thanks
P.S. Coffee is good.
I think I've understood your question. Pardon me if I didn't ;-)
super doesn't work that way. You can simply call,
[appDelegate.customViewController methodName];
The other way is to pass the reference of the customViewController to RandomName object, something like,
[[RandomName alloc] initWithParent:self];
You have to keep the reference of self in initWithParent method, lets say the variable name is parent, and call the method like this,
[parent methodName];

Iphone Error "request for member connectWeb in something not a structure or union"

When I call my urlconnection method from my (IBAction)buttonpressed method like this:
[self connectWeb];
I get error "request for member connectWeb in something not a structure or union"
but when I call the same method from my - (void)viewDidLoad method it works!?
Where is the connectWeb method in your implementation file?
I would guess that it is before viewDidLoad but after buttonPressed.
The reason for this is that the connectWeb method has been declared before viewDidLoad, so viewDidLoad is aware of it, whereas it's after the buttonPressed method, so the buttonPressed method isn't aware of it.
You have a couple of options.
Declare the method before any other method uses it.
- (void)connectWeb;
Then you can implement it anywhere within the implementation.
Move the implementation of connectWeb to before both viewDidLoad and buttonPressed - both the methods will then be aware of connectWeb.
A method missing from the header wouldn't cause this problem. Because Objective-C is dynamic, it will check if the instance implements that method at runtime, so as long as the method exists it will work. You would just get a compiler warning at build time.
Try deleting the line [self connectWeb] from your button delegate method and copy/pasting it from your viewDidLoad (or retyping it). From the error you're getting, it sounds like there might be an extra character in your statement. This happens to me from time to time, because I use synergy to share my keyboard & mouse between multiple computers.

Objective-C: call a method you just created

Simple question, as I am coming from another programming language. In Objective-C, lets say in a controller class I want to separate certain code into its own method, how do I call that method let's say, from viewLoad. As an example, let's say I create a method:
(void)checkIfInputCorrect
{
NSLog(#"text");
}
Now, i wanted to have in a delegate method, call this method. I tried [self checkIfInputCorrect] and get a warning saying Controller may not respond to -CheckIf...
I thought something like checkIfInputCorrect() would work that gives an error as well.
Basically how do you call a method?
Add this to your .h file
- (void)checkIfInputCorrect;
Call it with:
[self checkIfInputCorrect];
You need to list the method in the interface (ideal) or list the method implementation before the calling method (less ideal) so that the compiler can know that the class responds to the selector before it compiles the calling line.
To paraphrase Martin,
In your .m file, make sure your method -checkIfInputCorrect is placed so that it's physically above the method that has the line: [self checkIfInputCorrect];

Creating an object to act as a delegate - Objective C

My question is simple actually, how do I create an object to act as a delegate, instead of including the delegate methods in my view?
For example, I have x functionality that requires delegate methods, and they're currently setup to use self as the delegate. I'd like to put those methods in their own object so that the delegate methods can be called and do stuff if the view has ended.
What's the best way?
for example, NSXMLParser delegate methods - they exist, the delegate is defined, but I dont want to call them as self in my view controller... what other option do I have?
You can specify another custom class to handle the delegate methods, if you wish. Simply create a class, call it MyXMLParserDelegate or something similar. Then, all you have to do is tell your NSXMLParser object that it should use an instance of your class as its delegate.
If you are using Interface Builder, add a new object to the XIB file, set its class to MyXMLParserDelegate, and then drag a connection from your NSXMLParser object's delegate selector to the new object.
If you are doing it programmatically, the basic operation looks like this:
MyXMLParserDelegate * myDelegate = [[MyXMLParserDelegate alloc] init];
[someXMLParser setDelegate:myDelegate];
Keep in mind, however, that delegates are not retained, so in order to do this without leaking memory, you should add an ivar of type MyXMLParserDelegate to your viewController class, and then do the following:
// in your #interface block:
{
...
MyXMLParserDelegate * myDelegate;
}
// in your init method:
myDelegate = [[MyXMLParserDelegate alloc] init];
// in your awakeFromNib method (or anywhere else it seems appropriate):
[someXMLParser setDelegate:myDelegate];
// in your dealloc method:
[myDelegate release];
Check out this answer, I think it covers what you need: How to use custom delegates in Objective-C