试图在viewController之间传递数据 [英] Trying to pass data between viewControllers

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

问题描述

我在NavigationController里面有一个4个viewController的序列,每个都从用户那里获取一些文本输入的文本框,存储在NSMutableDictionary中。



在下一代的委托之前,它将自己作为下一代的代表,它也通过了NSMutDict。



这个工作正常。



我不明白的是:



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



如何将字典传回给VC1加上附加的信息,以便当它转过来到VC2再次有它的一切吗?



代理(VC1)有一个方法来更新其字典与VC2中的字典。



我还通过在VC1中的prepareForSegue:方法中设置VCB中的backBarButtonItem来定制。



I认为我越来越近了...



我只能通过在VC2中设置一个leftBarButtonItem并使用它而不是默认的返回按钮来获得目标操作。



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


$ b $我知道我不能在VC2中设置后退按钮,那我该怎么办?可以使用代理程序从VC2设置后退按钮的目标和动作吗?



我认为这可能与UINavigationBarDelegate有关,但我无法确定在哪里放什么。我尝试在VC2中设置它,但没有做任何事情。



TIA。



这里是相关的代码:



协议:

  #import< Foundation / Foundation .h> 

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

@end

从VC1.h:

  #importIAXAddNewUserDelegate.h

@interface IAXAddNewUser1:UITableViewController< UITextFieldDelegate,UIAlertViewDelegate,IAXAddNewUserDelegate>

@property(strong,nonatomic)NSManagedObjectContext * managedObjectContext;
@property(strong,nonatomic)User * selectedUser;
@property(强,非原子)用户* 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方法
- (void)updateNewUserDataWithData:(NSMutableDictionary *)newData
{
self。 userData = newData;
NSLog(@Updated AddUserVC1);
}

从VC2.m



$($)$($)$($)$($)$($)$($)
[self.delegate updateNewUserDataWithData:self.userData];
[self.navigationController popViewControllerAnimated:YES];
}


解决方案

所有其他词典的词典,尝试使用单身人士。你可以在这里看到一个例子: http://stackoverflow.com/a/9690731/542400



另外,这里有一些代码:



MainDictionary.h

  @interface MainDictionary:NSObject {
NSMutableDictionary * dictionary;
}

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

MainDictionary.m

  #importMainDictionary.h

static MainDictionary * sharedDictionary;

@implementation MainDictionary

- (id)init {
self = [super init];
dictionary = [[NSMutableDictionary alloc] init];
//如果你想添加任何初步到字典,在这里做
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和任何时间您要访问或设置该字典中的值(在本示例中,当您的textField结束编辑时),请执行以下操作:

  - (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中实现这一点,你很好去。 p>

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

解决方案

If you're updating all the dictionaries from all the other dictionaries, try using a singleton. You can see an example here: http://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.

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

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