-1

How can i write a global method that can be called anywhere in my iPhone App Can any one Help Please....

Mehul Mistri
  • 15,037
  • 14
  • 70
  • 94
Bad Boy
  • 628
  • 2
  • 5
  • 23

2 Answers2

3

You can write global functions (not part of a class) wherever you like. Just add the declaration where you use it (i.e. include a header file).

For example:

File globalfunctions.h

void doCoolStuff();

File globalfunctions.c

#include "globalfunctions.h"
void doCoolStuff()
{

}

and where you use it use

#include "globalfunctions.h" // or
#import "globalfunctions.h"
Eiko
  • 25,601
  • 15
  • 56
  • 71
  • 1
    Eiko's answer works, but keep in mind that if you want this to work on an iPhone, then you must name your file `globalfunctions.mm` not **.c** (in addition to of course, the .h file) or you will have compilation errors. Just did this last night. – casey Nov 02 '10 at 16:30
  • The correct file ending will only depend on what kind of language you are using. If ObjC, use '.m', if Obj-C++, use '.mm'. You will get away if '.c' if you don't use any Obj-C-ish things. – Eiko Nov 02 '10 at 17:14
  • Oh yeah, true. I was using some ObjC classes and couldn't compile until I made the extension `.mm`. But I also learned that, again, depending on if you are using Obj-Cish things or not you may need to reference this thread to modify your `.h` file to link properly. I just had trouble linking mine when I did this. And this solution worked. http://stackoverflow.com/questions/1362695/objective-c-symbol-not-found-strangeness – casey Nov 04 '10 at 06:10
1

You would normally use the Singleton Class to contain global data and methods for your iphone application. Here are 2 good references: Singleton Tutorial, Singleton Pattern

ennuikiller
  • 46,381
  • 14
  • 112
  • 137