Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

ios5 - Trying to pass data between viewControllers

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];
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you're updating all the dictionaries from all the other dictionaries, try using a singleton. You can see an example here: https://stackoverflow.com/a/9690731/542400

Also, here's some code:

MainDictionary.h

@interface MainDictionary : NSObject{
    NSMutableDictionary *dictionary;
}

+(MainDictionary *)sharedDictionary;
-(NSString *)getStringForKey:(NSString *)string;
-(void)setString:(NSString *)string forKey:(NSString *)key;
@end

MainDictionary.m

#import "MainDictionary.h"

static MainDictionary *sharedDictionary;

@implementation MainDictionary

-(id)init{
    self = [super init];
    dictionary = [[NSMutableDictionary alloc] init];
    // if you want to add anything preliminary to the dictionary, do it here
    return self;
}

+(MainDictionary *)sharedDictionary{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    sharedDictionary = [[self alloc] init];
    });
return sharedDictionary;
}
-(NSString *)getStringForKey:(NSString *)string{
    return [dictionary objectForKey:string];
}
-(void)setString:(NSString *)string forKey:(NSString *)key{
    [dictionary setValue:string forKey:key];
}
@end

Now #import MainDictionary.h, and any time you want to access or set values in that dictionary (in this example, when your textFields end editing), just do this:

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if(textField == textField1){
        [[MainDictionary sharedDictionary] setString: textField.text forKey:@"textField1"];
    }
}

or:

-(void)viewWillAppear{
    textField1.text = [[MainDictionary sharedDictionary] getStringForKey:@"textField1"];
    [super viewWillAppear:YES];
}

Implement this in each VC, and you're good to go.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...