russell
russell

Reputation: 3766

method call between different class in Objective c

Is it possible to call method from one class to another where two classes from two different parent class. Suppose one from UIImage class and another from UIViewController class?

Upvotes: 1

Views: 1658

Answers (2)

program247365
program247365

Reputation: 4019

You probably want to do something like what is said in this answer of another question:

Objective-C : Call a method from another class in addTarget:action:forControlEvents

Upvotes: 0

Gordon Seidoh Worley
Gordon Seidoh Worley

Reputation: 8068

Yes, one of the cool things about Objective-C is that you can call a method on any object, even if it doesn't respond. Although not as robust as in Objective-C's father, SmallTalk (yes, SmallTalk is the dad and C is the mom, don't ask), it's still pretty powerful. So let's say you have something like this:

- (void)doSomethingToThis: (id)thisObject
{
  [thisObject doSomething];
}

Then later...

//...
  UIImage *thisImage = ...
  UIViewController *thisController = ...

  [self doSomethingToThis: thisImage];
  [self doSomethingToThis: thisController];
//...

Something like this will compile just fine, but be warned if UIImage and UIViewController don't both respond to doSomething, then you could end up with a crasher (depends on how you have the compiler flags set, but I believe by default you'll get a crash).

Upvotes: 1

Related Questions