试图在视图控制器之间传递数据 [英] Trying to pass data between viewControllers

查看:18
本文介绍了试图在视图控制器之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 NavigationController 中有一系列 4 个视图控制器,每个视图控制器都从用户那里获取一些文本字段,这些文本字段存储在 NSMutableDictionary 中.

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.

每个 VC 的集合在 segue 之前都将自己作为 nextVC 的代表,它还传递 NSMutDict.

Each of the VC's set's itself up as the delegate of the nextVC before it segues, it also passes the NSMutDict along.

这很好用.

我不明白的是:

假设我已经填写了 VC1 中的 5 个文本字段.然后我将自己设置为 VC2 的代表,将带有输入数据的字典传递给 VC2,然后转到 VC2.在 VC2 中,我填写了另外 4 个文本字段并将它们添加到字典中.如果我决定需要更改 VC1 中的某些内容,我会点击后退按钮并修改数据.但是当我再次前进时,我丢失了我在 VC2 上输入的内容.

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.

如何将字典与添加的信息一起传递回 VC1,以便当它再次转发到 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?

委托 (VC1) 有一个方法可以用 VC2 中的字典更新其字典.

The delegate (VC1) has a method to update its dictionary with the dictionary in VC2.

我还自定义了 VC2 中的 backBarButtonItem,方法是在 VC1 的 prepareForSegue: 方法中设置它.

I have also customised the backBarButtonItem in VC2 by setting it in the prepareForSegue: method in VC1.

我想我已经接近了,但是......

I think I'm getting close but...

我只能通过在 VC2 中设置 leftBarButtonItem 并使用它而不是默认的后退按钮来使目标操作起作用.

I can only get the target actions to work by setting a leftBarButtonItem in VC2 and using that instead of the default back button.

在 VC1 (prepareForSegue:) 中设置后退按钮似乎不允许设置任何目标或操作.

Setting the back button in VC1 (prepareForSegue:) doesn't seem to allow any target or action to be set.

我知道我无法在 VC2 中设置后退按钮,我该怎么办?我可以使用委托从 VC2 设置后退按钮的目标和操作吗?

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?

我认为这可能与 UINavigationBarDelegate 有关,但我不知道该放在哪里.我尝试在 VC2 中设置它,但它没有做任何事情.

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.

以下是相关代码:

协议:

#import <Foundation/Foundation.h>

@protocol IAXAddNewUserDelegate <NSObject>
@required
- (void)updateNewUserDataWithData: (NSMutableDictionary *)newData;

@end

来自 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

来自 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");
}

来自 VC2.m

-(void)passDataBack:(id)sender
{
    NSLog(@"Sending Data Back to VC1");
    [self.delegate updateNewUserDataWithData:self.userData];
    [self.navigationController popViewControllerAnimated:YES];
}

推荐答案

如果您要更新所有其他词典中的所有词典,请尝试使用单例.您可以在此处查看示例:https://stackoverflow.com/a/9690731/542400

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

MainDictionary.h

@interface MainDictionary : NSObject{
    NSMutableDictionary *dictionary;
}

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

MainDictionary.m

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

现在#import MainDictionary.h,只要您想访问或设置该字典中的值(在本例中,当您的文本字段结束编辑时),只需执行以下操作:

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"];
    }
}

或:

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

在每个 VC 中实现这一点,你就可以开始了.

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

这篇关于试图在视图控制器之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆