简体   繁体   中英

XCode methods file

How can ai 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
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM