在XIB之间共享数据

时间:2010-10-28 13:34:39

标签: iphone objective-c

我正在关注James Brannan教程,我试图在一些xbis之间共享数据。没有运气。

我有2个xib。第一个,简单的按钮和文本字段。第二个,只是一个标签,用于显示第一个xib文本字段的结果。

所以,我不知道我做错了什么。我在教程中使用NSObject。

SharedData.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

@interface SharedData : NSObject {

NSString *MeuNome;

}

@property (nonatomic, retain) NSString *MeuNome;

@end

SharedData.m

 #import "SharedData.h"


 @implementation SharedData

 @synthesize MeuNome;

 - (void) dealloc {

self.MeuNome = nil;
[super dealloc];

 }

 @end

FirstStepViewController.h

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

#import "SharedData.h"


@interface FirstStepViewController : UIViewController {

IBOutlet SharedData *sharedData;
IBOutlet UITextField *campoNome;


}

@property (nonatomic, retain) UITextField * campoNome;
@property (nonatomic, retain) SharedData *sharedData;

- (IBAction) takeNextStep: (id) sender;

@end

FirstStepViewController.m

#import "FirstStepViewController.h"
#import "SecondStepViewController.h"
#import "LuconeAppDelegate.h"


@implementation FirstStepViewController

@synthesize campoNome, sharedData;


- (IBAction) takeNextStep : (id) sender{
// declaracao de shared data
[sender resignFirstResponder];

self.sharedData.MeuNome = self.campoNome.text;

// faz animacao para proximo slide

SecondStepViewController *varSecondViewController = [[SecondStepViewController
                                                              alloc] initWithNibName:@"SecondStepViewController" bundle:nil ];
[self.navigationController pushViewController:varSecondViewController
                                     animated: YES];
[self navigationController].navigationBarHidden = NO;


}

- (void)viewDidLoad {

[self navigationController].navigationBarHidden = YES;

[super viewDidLoad];


}

- (void)dealloc {
self.sharedData = nil;
//self.campoNome = nil;
[super dealloc];
}


@end

SecondStepViewController.h

#import <UIKit/UIKit.h>
#import "SharedData.h"


@interface SecondStepViewController : UIViewController {

IBOutlet SharedData *sharedData;
IBOutlet UILabel *nome;

}

@property (nonatomic, retain) SharedData *sharedData;
@property (nonatomic, retain) UILabel *nome;

@end

SecondStepViewController.m

#import "SecondStepViewController.h"
#import "SharedData.h"


@implementation SecondStepViewController

@synthesize nome, sharedData;

- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"step two";
self.nome.text = self.sharedData.MeuNome;
}

- (void)dealloc {
self.sharedData = nil;
[super dealloc];
}


@end

怎么了?

谢谢!

1 个答案:

答案 0 :(得分:0)

您的代码有三个小问题。

  1. IBOutlet仅适用于使用界面构建器放置在视图上的控件,例如UIButtons,UILabels。因此,SharedData的实例不必是IBOutlets。此外,如果以编程方式创建界面,则再次使用IBOutlet是不必要的。

  2. 您声明sharedData是SharedData类的实例,但您没有实例化它。在设置任何sharedData属性之前,在FirstStepViewController.m中,您应该添加以下代码:

    sharedData = [[sharedData alloc] init]; 之后你可以这样做:

    self.sharedData.MeuNome = self.campoNome.text; 如果省略“自我”。代码应该可以正常工作。

  3. 最后,在将第二个视图控制器推送到导航堆栈之前,您必须将第一个视图控制器中的sharedData对象分配给第二个视图控制器中的sharedData。

  4. 在你的FirstStepViewController.m中

    添加:

    [varSecondViewController sharedData] = [self sharedData];
    

    之前:

    [self.navigationController pushViewController:varSecondViewController
                                         animated: YES];
    

    最后确保您已在界面构建器中正确连接所有插座,并且所有内容都应完美运行,然后:)