0

How can a i create a .m file that houses all of my methods for my iphone app?

I need these methods to be able to be called from any view.

Example Method:

-(void)loadinfo(id):sender{
//Dynamically load UI Stuff
}
Mattigins
  • 1,014
  • 9
  • 25
  • Why should you do this? Do you need a class that will share it's methods with different classes? – John Smith Jul 18 '12 at 09:47
  • 1
    Use [Singleton Class](http://jason.agostoni.net/2012/01/22/ios-best-practices-singletons/) to share global methods and variable that you use all around your app.. – Bala Jul 18 '12 at 09:51
  • Yes i need a class that has a method in it that is able to be called from other classes.. – Mattigins Jul 18 '12 at 09:56
  • @user1533419 Take a look at class methods ([link](http://stackoverflow.com/q/1053592/335858)). – Sergey Kalinichenko Jul 18 '12 at 10:20
  • Ok so i just change the - to a +? When i do that the parts that call [self bah]; in the method come up as errors. – Mattigins Jul 18 '12 at 10:35
  • Check out my answer to your previous question: http://stackoverflow.com/questions/11533535/xcode-dynamically-loaded-gui-for-all-views/11546088#11546088. – Roger Jul 18 '12 at 21:28

1 Answers1

0

It sounds like what you want is achievable though the normal design pattern of creating a custom subclass of NSObject. There's no need to do anything fancy, like using class methods or writing as a singleton, but you can if you want.

All you need to do is create a standard NSObject-derived subclass containing all the methods (class or instance) you need, and then import that class whenever you need to use one of its methods (either by invoking the class method directly, or by instantiating an object of the class and calling the method on the object in the normal way). So, import the class in the desired file:

#import "YourSubclass.h"

Then, when you want to call a method:

[YourSubclass yourClassMethod];  // If a class method

or

YourSubclass *subclass = [[YourSubclass alloc] init];   // If an instance method
[subclass yourInstanceMethod];
//[subclass release];  // Uncomment if *not* using ARC

You could also implement this subclass as a singleton, though some feel that this design pattern is not a great way to go (Luke Redpath states this view in his article on singletons), so whether you choose to go that way is up to you.