Search code examples
objective-cobjective-c-category

Dynamically call a method from another class in Objective C


I am trying to call a method from another class which is in another source file.

Example:

  1. I have a file named Source1.h/Source1.m (both are objective C class files)
  2. I have another file names Source2.h/Source2.m (both are objective C class files)
  3. Source1 contains 2 methods eg: method1 and method2.
  4. From Source2 file, I need to call the method1 from source1 file.

I know how to do that in objective C. But in my source2 file, the method names in Source1 will be retrieved dynamically. I am not going to hard code it some thing similar to this.

Source1 *a = [[Source1 alloc]init];

[a method1];

The method1 text will taken from a text file. I can use Selector to invoke a method from same class. But I can't use selector to call method from another class.

Please help to solve this issue....

Many Thanks......


Solution

  • This answer seems to be relevant: How can I dynamically create a selector at runtime with Objective-C?

    Basically you can use NSSelectorFromString to make a selector from an UTF8 string in an NSString.