This weekend I set out to understand how to pass data back from ViewController2 to ViewController1 when I exit VC2. I found a good explanation on line with helper code but I am not understanding a few things even though it works.
So in VC1.h I import the header of VC2. I created in the xib a UIlabel and a UIbutton. The user presses the button and it pushes VC2 on the stack, easy
VC1.m
In VC2 I just have a textField, that is it. The user enters some string and presses the back button on the NavController (NavController set up in the AppDelete).
in the VC2.m I simply get the string in the textField and add it to an array and it is sent back to VC1 and displayed in the UILabel.
It all works but a few things I don't get why?
Q1. When VC2 is instantiated a @protocol is defined with 1 Method. In the @interface, in the ivar section, dose that then creating an object called muDelegate of type MyUsefulDelegate which is the protocol? I don't understand the 'id' part of that line? I know id is a generic object type and used in Methods, like in the Protocols method they use id. But it seems to be setting a return type in the ivar section where you declare your global instance variable, I don't get that?
Q2. When the button is pressed, in VC1.m the VC2 object is instantiated. Since I declared an muDelegate object in the ivar section I gain access to that and then set it to 'self'. This has me totally confused, self? I could see this [svc setMuDelegate.delegate:self]; There you are telling the delegate to be 'self', or in charge of it's self.
The @property section I get, it is creating the setters and getters to access this Method.
Q3. After I get my string from the textField and add it to myArray there is the line of code
When I normally do something to my objects, like add to myArray(if it was a mutableArray) I would write something like [array addObject:someObject]; But if I try and write code like this,
The code won't work. Why must I refer to that object with 'self' first? I don't refer to myArray that way [[self myArray] addObject: someObject];?
Q4. That new @protocol what was created, that is now destroyed when I exit out of VC2 is gone, no longer accessible? Every time I instantiate a new VC2 a new @protocol is created. I am pretty sure that is right.
So in VC1.h I import the header of VC2. I created in the xib a UIlabel and a UIbutton. The user presses the button and it pushes VC2 on the stack, easy
Code:
#import <UIKit/UIKit.h>
#import "SecondVC.h"
@interface ViewController : UIViewController [COLOR="Red"]<MyUsefulDelegate>[/COLOR]{
IBOutlet UILabel *displayLable;
}
-(IBAction)pressButton:(id)sender;
@end
Code:
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad
//...Boilerplate code removed.
-(IBAction)pressButton:(id)sender{
SecondVC *svc = [[SecondVC alloc] init];
[COLOR="Red"] [svc setMuDelegate:self];[/COLOR]
[self.navigationController pushViewController:svc animated:YES];
}
- (void)infoReturned:(id)objectReturned {
NSArray *tempArray = [NSArray arrayWithArray:objectReturned];
displayLable.text = [tempArray objectAtIndex:0];
}
@end
In VC2 I just have a textField, that is it. The user enters some string and presses the back button on the NavController (NavController set up in the AppDelete).
Code:
#import <UIKit/UIKit.h>
[COLOR="Red"]@protocol MyUsefulDelegate <NSObject>
- (void)infoReturned:(id)objectReturned;
@end
[/COLOR]
@interface SecondVC : UIViewController{
[COLOR="Red"]id <MyUsefulDelegate> muDelegate;[/COLOR]
IBOutlet UITextField *textField;
}
@property (assign) id <MyUsefulDelegate> muDelegate;
@end
in the VC2.m I simply get the string in the textField and add it to an array and it is sent back to VC1 and displayed in the UILabel.
Code:
#import "SecondVC.h"
@implementation SecondVC
//...Boilerplate code removed.
-(void)viewWillDisappear:(BOOL)animated{
NSArray *myArray =[[NSMutableArray alloc] initWithObjects:textField.text, nil];
[COLOR="Red"] [[self muDelegate] infoReturned:myArray];[/COLOR]
}
@end
It all works but a few things I don't get why?
Q1. When VC2 is instantiated a @protocol is defined with 1 Method. In the @interface, in the ivar section, dose that then creating an object called muDelegate of type MyUsefulDelegate which is the protocol? I don't understand the 'id' part of that line? I know id is a generic object type and used in Methods, like in the Protocols method they use id. But it seems to be setting a return type in the ivar section where you declare your global instance variable, I don't get that?
Q2. When the button is pressed, in VC1.m the VC2 object is instantiated. Since I declared an muDelegate object in the ivar section I gain access to that and then set it to 'self'. This has me totally confused, self? I could see this [svc setMuDelegate.delegate:self]; There you are telling the delegate to be 'self', or in charge of it's self.
The @property section I get, it is creating the setters and getters to access this Method.
Q3. After I get my string from the textField and add it to myArray there is the line of code
Code:
[[self muDelegate] infoReturned:myArray];
Code:
[muDelegate infoReturned:myArray];
Q4. That new @protocol what was created, that is now destroyed when I exit out of VC2 is gone, no longer accessible? Every time I instantiate a new VC2 a new @protocol is created. I am pretty sure that is right.