9

This a very basic question but I've searched all over and been unable to find an answer that explains well enough for me to get my head around it.

What I want to do is create a method in one class of my iOS app and then call that method from other classes in my app. Could someone explain exactly what I need to do to achieve this? Any help would be greatly appreciated as all my attempts so far have failed!

Thanks.

Matt Ledger
  • 213
  • 1
  • 3
  • 6
  • 2
    I think your question is a little vague. You will have already done this in any basic app. ie. calling methods on objects. Can you give code samples showing what you are trying to do? – drekka Mar 16 '12 at 02:47
  • You can use NSNotificationCenter or Delegates or your custom protocols.... – Abdul Yasin Sep 26 '13 at 13:27

3 Answers3

45

Objective-C:

You have to import the header of the class that contains the method you want to use (ClassYouWantToUse.h) into the class you want to use it at (TargetClass).

Inside the TargetClass.h or TargetClass.m (depending on the scope you want to give it):

#import "ClassYouWantToUse.h"

Then create an instance of the class you want to use inside the target class either as a property like this:

@property (nonatomic,strong) ClassYouWantToUse *classObject;

Or as an instance variable like this:

ClassYouWantToUse *classObject;

Make sure you initialize it! (usually inside ViewDidLoad):

classObject = [[ClassYouWantToUse alloc] init];

Now you can call any public methods from that class like this:

[classObject theClassMethodWithParam:param1 andSecondParam:param2];

Note: The ClassYouWantToUse class must have the methods that you want to make accessible to others by declaring them in the header file:

- (void)theClassMethodWithParam:(UIImage*)someImage andSecondParam:(NSString*)someText;

Otherwise you won't be able to see these methods.


Swift:

Theres really nothing special about it in swift, just adding this as a reference.

In swift you simply create an instance of the class you want to use:

let classObject = ClassYouWantToUse()

And use it directly:

classObject.theClassMethodWithParam(param1, andSecondParam:param2)
Pochi
  • 13,391
  • 3
  • 64
  • 104
  • Thank you **so** much! You can't imagine how many posts I've read trying to figure this out. Working just as I wanted now! – Matt Ledger Mar 16 '12 at 12:21
  • Curious though what is the proper solution to now having two instances of ClassToBeUsed? Because *something is not the same as self back in ClassToBeUsed? So in this example if I would reference self from [something somethingWithParam:blabla andSecondParam:otherbla] it would reference *something. What if I need to reference the original instance of ClassToBeUsed? – rick Jul 12 '13 at 01:26
  • Class to be used doesnt exist, per se, it is a class to be used when it is initialized. you cannot reference a class that has not been initialized, unless you call the class methods. But these are class specific so they dont hold instance related variables, or data, they always do the same. if you mean, call from somewhere else the same one then you have to access the first you made specifically, or use a singleton. – Pochi Jul 16 '13 at 04:02
3

You have two basic options. You can either create or pass-in an instance of the first class to the second class, or you can add a static method to the first class and call it directly using the class object.

For instance, say you have:

@interface ClassA : NSObject {
}

//instance methods
- (int) addNumber:(int)num1 withNumber:(int)num2;

//static/class methods
+ (int) add:(int)num1 with:(int)num2;
@end

@implementation ClassA
- (int) addNumber:(int)num1 withNumber:(int)num2 {
    return num1 + num2;
}

+ (int) add:(int)num1 with:(int)num2 {
    return num1 + num2;
}
@end

Then you can do:

#import "ClassA.h"

@interface ClassB : NSObject {
    ClassA* adder;
}

//constructors
- (id) init;  //creates a new instance of ClassA to use
- (id) initWithAdder:(ClassA*)theAdder;  //uses the provided instance of ClassA

//instance methods
- (int) add2To:(int)num;

//static/class methods
+ (int) add3To:(int)num;
@end

@implementation ClassB
- (id) init {
    if (self = [super init]) {
        adder = [[ClassA alloc] init];
    }
    return self;
}

- (id) initWithAdder:(ClassA*)theAdder {
    if (self = [super init]) {
        adder = theAdder;
    }
    return self;
}

- (int) add2To:(int)num {
    return [adder addNumber:2 withNumber:num];
}

+ (int) add3To:(int)num {
    return [ClassA add:3 with:num];
}
@end

Note that in most cases, you would use instance methods rather than static methods.

aroth
  • 54,026
  • 20
  • 135
  • 176