How to call a method from one class to another (iOS)

This is a very simple question, but I was looking for it and could not find an answer that explains me well enough for me to hug it.

What I want to do is create a method in one class of my iOS application, and then call this method from other classes in my application. Can someone explain what I need to do for this? Any help would be greatly appreciated, since all my attempts have so far failed!

Thanks.

+7
source share
3 answers

Objective-C:

You need to import the class header that contains the method you want to use (ClassYouWantToUse.h) into the class you want to use in (TargetClass).

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

#import "ClassYouWantToUse.h" 

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

 @property (nonatomic,strong) ClassYouWantToUse *classObject; 

Or as an instance variable:

 ClassYouWantToUse *classObject; 

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

 classObject = [[ClassYouWantToUse alloc] init]; 

Now you can call any public methods from this class as follows:

 [classObject theClassMethodWithParam:param1 andSecondParam:param2]; 

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

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

Otherwise, you will not be able to see these methods.


Swift:

In fact, there is nothing special about this issue, just adding this as a link.

In the quick version, you simply create an instance of the class you want to use:

 let classObject = ClassYouWantToUse() 

And use it directly:

 classObject.theClassMethodWithParam(param1, andSecondParam:param2) 
+43
source

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

For example, let's say that 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 should use instance methods, not static methods.

+3
source

All Articles