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

后端 未结 3 1100
闹比i
闹比i 2020-12-01 05:51

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

相关标签:
3条回答
  • 2020-12-01 06:22

    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.

    0 讨论(0)
  • 2020-12-01 06:28

    You have to use the concept of delegation.

    https://developer.apple.com/library/ios/#documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html

    0 讨论(0)
  • 2020-12-01 06:41

    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)
    
    0 讨论(0)
提交回复
热议问题