I have a sequence of 4 viewControllers inside a NavigationController, each grabs a few textFields of input from the user which are stored in a NSMutableDictionary.
Each of the VC's set's itself up as the delegate of the nextVC before it segues, it also passes the NSMutDict along.
This works fine.
What I don't understand is this:
Say I have filled in the 5 textFields in VC1. Then I set myself as the delegate of VC2, pass VC2 the dictionary with the input data and segue to VC2. In VC2 I fill in another 4 textFields and add these to the dictionary. If I then decide I need to change something in VC1 I tap the back button and amend the data. But when I go forwards again I lose the stuff I input on VC2.
How do I pass the dictionary back to VC1 with the added info so that when it gets passed forwards to VC2 again it has everything in it?
The delegate (VC1) has a method to update its dictionary with the dictionary in VC2.
I have also customised the backBarButtonItem in VC2 by setting it in the prepareForSegue: method in VC1.
I think I'm getting close but...
I can only get the target actions to work by setting a leftBarButtonItem in VC2 and using that instead of the default back button.
Setting the back button in VC1 (prepareForSegue:) doesn't seem to allow any target or action to be set.
I know I can't set the back button in VC2, so what can I do? Can I set the target and action of the back button from VC2 using the delegate?
I think it may be something to do with UINavigationBarDelegate but I can't figure out where to put what with that. I tried setting it up in VC2 but it didn't do anything.
TIA.
Here's the relevant code:
Protocol:
#import <Foundation/Foundation.h>
@protocol IAXAddNewUserDelegate <NSObject>
@required
- (void)updateNewUserDataWithData: (NSMutableDictionary *)newData;
@end
From VC1.h:
#import "IAXAddNewUserDelegate.h"
@interface IAXAddNewUser1 : UITableViewController <UITextFieldDelegate, UIAlertViewDelegate, IAXAddNewUserDelegate>
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
@property (strong, nonatomic) User *selectedUser;
@property (strong, nonatomic) User *aNewUser;
@property BOOL isFirstUser;
- (void)updateNewUserDataWithData: (NSMutableDictionary *)newData;
@end
From VC1.m:
#pragma mark - Segues
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"AddUser2"]) {
        IAXAddNewUser2 *addUser2VC = segue.destinationViewController;
        addUser2VC.managedObjectContext = self.managedObjectContext;
        addUser2VC.progressTotal = self.progressTotal;
        addUser2VC.isFirstUser = self.isFirstUser;
        addUser2VC.userData = self.userData;
        addUser2VC.delegate = self;
        if (self.selectedUser) {
            addUser2VC.selectedUser = self.selectedUser;
        }
        self.title = @"Step 1";
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" 
                                                                       style:UIBarButtonItemStyleBordered 
                                                                      target:self 
                                                                      action:@selector(passDataBack:)];
        self.navigationItem.backBarButtonItem = backButton;
    }
}
#pragma mark - IAXAddNewUserDelegate Methods
- (void)updateNewUserDataWithData: (NSMutableDictionary *)newData
{
    self.userData = newData;
    NSLog(@"Updated AddUserVC1");
}
From VC2.m
-(void)passDataBack:(id)sender
{
    NSLog(@"Sending Data Back to VC1");
    [self.delegate updateNewUserDataWithData:self.userData];
    [self.navigationController popViewControllerAnimated:YES];
}