0

大家好,我在视图之间传递值时遇到了很多麻烦,

我有一个带有 3 个文本框 FirstName LastName 电子邮件的 StartViewController 我想将它们的值传递给 ViewController 我尝试了一些不同的东西,但我所做的似乎没有任何工作我试过这个: http ://highscoreapps.blogspot.com/ 2012/05/passing-data-between-view-controllers.html http://www.theappcodeblog.com/2011/04/15/passing-data-between-views-tutorial-using-a-protocol-delegate-in -你的 iPhone 应用程序/

但我似乎无法得到任何工作。到目前为止,这是我的一些代码:

在 StartViewControler.h

@protocol DataPassingControllerDelegate <NSObject>

-(void)passDataMethod:(NSString *)TextFirstName Data2:(NSString *)TextLastName Data3:(NSString *)TextEmail;

@property(nonatomic,retain) IBOutlet UITextField *uTextFirstName;
@property(nonatomic,retain) IBOutlet UITextField *uTextLastName;
@property(nonatomic,retain) IBOutlet UITextField *uTextEmail;

@property (nonatomic, strong) NSString *sTextFirstName;
@property (nonatomic, strong) NSString *sTextLastName;
@property (nonatomic, strong) NSString *sTextEmail; 

@property (nonatomic, unsafe_unretained) id<DataPassingControllerDelegate> delegate;

在 StartViewController.M

-(IBAction)ButtonTakeSurvey:(id)sender{
self.sTextFirstName = uTextFirstName.text;
self.sTextLastName = uTextLastName.text;
self.sTextEmail = uTextEmail.text;

[self.delegate passDataMethod:sTextFirstName Data2:sTextLastName Data3:sTextEmail];

ViewController *myView1 = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
[self.view addSubview:myView1.view];
}

在 ViewController.HI 中添加了 #import "StartViewController.h"

@interface ViewController : UIViewController <DataPassingControllerDelegate> 

如何在 ViewController.m 上获取 firstName LastName 电子邮件我希望它填充我已加载的 3 个局部变量。

谢谢!

4

3 回答 3

2

我通常使用单例类来处理 ViewController 之间的通信。单例模式允许您将数据与控制器本身分开。下面是我使用的代码版本。

数据管理器.h

#import <Foundation/Foundation.h>

@interface DataManager : NSObject

+(DataManager*)sharedInstance;
-(void) setObject:(id)anObject forKey:(id<NSCopying>)aKey;
-(id) objectForKey:(id)aKey;
-(void) removeAllObjects;
@end

数据管理器.m

#import "DataManager.h"

@implementation DataManager

static DataManager *sharedInstance;
static NSMutableDictionary* dictionary;

- (id)init {
    self = [super init];
    if (self) {
        dictionary = [[NSMutableDictionary alloc] initWithCapacity:0];
    }
    return self;
}


+ (DataManager *) sharedInstance
{
    @synchronized(self)
    {
        if (sharedInstance == NULL)
        {
            sharedInstance = [[DataManager alloc] init];
        }
    }

    return sharedInstance;
}


-(void) setObject:(id)anObject forKey:(id<NSCopying>)aKey
{
    [dictionary setObject:anObject forKey:aKey];
}

-(id) objectForKey:(id)aKey
{
    return [dictionary objectForKey:aKey];
}

-(void) removeAllObjects
{
    [dictionary removeAllObjects];
}

@end

为了使用它,您所要做的就是在第一个视图控制器中,获取 sharedInstance 并在推送(或在模态 VC 的情况下呈现)ViewController 之前设置值。

DataManager* dm = [DataManager sharedInstance]
[dm removeAllObjects];
[dm setObject:firstNameString forKey:@"firstName"];
[dm setObject:lastNameString forKey:@"lastName"];
[dm setObject:emailString forKey:@"email"];

然后在第二个 ViewController 的 ViewDidLoad (或 viewWillAppear 或其他)中,您可以从单例中获取值,例如:

DataManager* dm = [DataManager sharedInstance]
NSString* firstName = (NSString*)[dm objectForKey:@"firstName"];
NSString* lastName = (NSString*)[dm objectForKey:@"lastName"];
NSString* email = (NSString*)[dm objectForKey:@"email"];
//do stuff with the values.

如果您想了解更多关于单例的信息(如果您目前不熟悉该模式,我会推荐它)维基百科文章非常好,并且有许多 OO 设计站点对其进行了更详细的讨论。

于 2013-03-04T18:08:41.790 回答
0

我建议将数据模型与您的控制器分开。有很多方法可以做到这一点,但一种简单的方法是创建一个“用户配置文件”类作为单例(跨应用程序的共享实例)并将数据存储在那里。这样,每个需要该数据的控制器都可以自己访问它。

于 2013-03-04T17:48:35.493 回答
0

在我看来,您需要做的就是添加passDataMethod到您的 ViewController.m 中:

-(void)passDataMethod:(NSString *)TextFirstName Data2:(NSString *)TextLastName Data3:(NSString *)TextEmail
{
    NSString *myLocalVar1 = [NSString stringWithString:TextFirstName];
    NSString *myLocalVar2 = [NSString stringWithString:TextLastName];
    NSString *myLocalVar3 = [NSString stringWithString:TextEmail];

    // Do something useful with myLocalVar1, 2, and 3 here
}
于 2013-03-04T18:02:41.263 回答