Class A (ViewController) wants to communicate with Class B (ViewController)
Before calling Class B, do the following:
In Class B, create a @property NSString* receivedValue;
In Class A,
#import Class B
then in the method that calls B (either prepareForSegue or didSelectRowAtIndexPath or some method where you present Class B view controller, do the following:
Class B *calledVC = alloc/init
calledVC.receivedValue = ‘whatever value you want to pass’;
then call Class B VC
Done!
DELEGATES
Let’s say Class B wants to notify Class A that something has finished. In Class B add this above your @interface:
@protocol YourDelegateProtocol <NSObject>
– (void)itemWillBePassed;
@end
Then in the regular Class B @interface
@property (nonatomic, weak) id <YourDelegateProtocol> delegate;
This means that Class B has a delegate protocol declared and anyone who wants to be its delegate needs to adopt this protocol and the methods defined herein.
So now in your Class A, right on the @interface line, add <YourDelegateProtocol> right after the:
@interface ViewController : UIViewController
And since now Class A implements Class B’s protocol, it must implement the delegate methods, which is only itemWillBePassed in our example.
Now simply, whenever Class B needs to notify Class A, it has to call:
[self itemWillBePassed];
And Class A will know about it in its implemented delegate method!
A complex subject to wrap your head around, i agree! But its pretty cool once you get a working example going.
Let’s think of a game where a lander comes down from the sky (presumably on Mars – since Mars is a hot topic lately) and deploys a rover. In Cocos2d, you put all your objects on a Layer object. This means the layer class is the main class (analogous to a viewcontroller class contains buttons, views, labels, cells etc).
You create a Lander object class because you want to re-use it everywhere in the game. With every new level, you must create a new layer class but you can reuse your Lander Class. So you wan’t your Lander to be able to deploy these rovers so that each time you reuse the lander, it can deploy rovers on any given layer.
Let’s look at this image:
Marcio Valenzuela at Santiapps.com
The Lander object declares a GameplayLayerDelegate property. This means any lander object will be a delegate to the GameplayLayer. This means the Lander can call the mystery protocol method in order to create the Rover object by saying:
[delegate mysteryMethod];
And the GameplayLayer only has to adopt the GameplayLayerDelegate.
In the case of an iOS app, a typical scenario might be a To Do List where you have a list of items and you wish to add a new item. You have 2 view controllers: one lists the existing items and the other is called when the user taps on the “Add” button and wants to add a new item. You might have something that looks like this:
Delegates & Protocols: Santiapps.com by Marcio Valenzuela
In this scenario the AddItemViewController on the right will want to take the user input and report it back to the ViewController on the left labeled To Do List. The steps are:
1) The ViewController on the left will conform to or adopt the AddItemViewControllerDelegate.
2) The AddItemViewController class declares its protocol like so: