Search code examples
iphoneobjective-cios4nsnotificationcenter

Sending a notification when the operation has been completed in one class?


I have two classes, Class A and Class B.

Class A has a method

 -(void)methodA:(id)sender
{

}

and Class B has a method

-(void)methodB:(id)sender
{

}

Now i have some work is happening in methodA ,So once it is completed i want to send a notification from methodA: to methodB: So i can do some operation on the basis of notification.

So how can i do this? Can anybody guide me as i am new to obj-c?


Solution

  • Use delegate. Simple code from wiki: visit http://en.wikipedia.org/wiki/Delegation_pattern

    • A delegate is an object whose objects are (generally) called to handle or respond to specific events or actions.
    • You must "tell" an object which accepts a delegate that you want to be the delegate. This is done by calling [object setDelegate:self]; or setting object.delegate = self; in your code.
    • The object acting as the delegate should implement the specified delegate methods. The object often defines methods either in a protocol, or on NSObject via a category as default/empty methods, or both. (The formal protocol approach is probably cleaner, especially now that Objective-C 2.0 supports optional protocol methods.)
    • When a relevant event occurs, the calling object checks to see if the delegate implements the matching method (using -respondsToSelector:) and calls that method if it does. The delegate then has control to do whatever it must to respond before returning control to the caller.