问题
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?
回答1:
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).
回答2:
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
来源:https://stackoverflow.com/questions/1496281/method-call-between-different-class-in-objective-c