Chris
Chris

Reputation: 1539

Objective C how to communicate between objects?

trying to figure out a best way of doing things. I'm new to objective c as a language.

I have object class A, and object class B and would like A to call a method on class B and once that method is done to have B call A back tell the result.

What I'm doing now is on class A adding self to NSNotificationCenter. Than having class B post that Notification. It works but it seems like an overkill for this type of simple process.

Is it legal to simply pass pointer of self to class B? something like?

// from class A
- (void)methodInClassA
{
B * b= [[B alloc]init];
[b callMethod:self];
[b release];
b = nil;
}

where class B would be
- (void)callMethod:(A*)sender
{
    [sender resultCallbackMethod];
}

Upvotes: 2

Views: 1312

Answers (1)

dive
dive

Reputation: 2210

just use protocols (Swift 3.1). here is well explained.

Protocols declare methods that can be implemented by any class. Protocols are useful in at least three situations:

  • To declare methods that others are expected to implement
  • To declare the interface to an object while concealing its class
  • To capture similarities among classes that are not hierarchically related

Upvotes: 3

Related Questions