xcode在视图之间传递3个nsstring值时遇到麻烦

时间:2013-03-04 17:37:22

标签: iphone ios objective-c nsview

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

我有一个带有3个文本框的StartViewController FirstName LastName电子邮件我想将它们的值传递给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-your-iphone-app/

但我似乎无法得到任何工作。 以下是我目前的一些代码:

在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.H中添加了

    #import“StartViewController.h”

@interface ViewController : UIViewController <DataPassingControllerDelegate> 

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

谢谢!

3 个答案:

答案 0 :(得分:2)

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

DataManager.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

DataManager.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设计网站会更详细地讨论它。

答案 1 :(得分:0)

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

答案 2 :(得分: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
}