views:

29

answers:

1

Hey everbody.

I'm following the James Brannan tutorial's, and im trying to share data between some xbis. No luck.

I have 2 xib's. The first, simple button and textfield. The second, just a label, to show the result of the first xib textfield.

So, i dont know what im doing wrong. Im using NSObject like in tutorial.

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

What's wrong?

Thanks!

A: 

Hello,

There are three small problems with your code.

1.IBOutlet is only for controls that you put on your view using interface builder e.g. UIButtons, UILabels. So it is unnecessary for instances of SharedData to be IBOutlets.

Furthermore if you create your interface programmatically then using IBOutlet is again unnecessary.

2.You Declare that sharedData is an instance of SharedData class but you do not instantiate it. in your FirstStepViewController.m before you set any of sharedData's properties you should add the following code:

sharedData = [[sharedData alloc] init];

after that you can do:

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

if you omit the "self." the code should work just as fine.

3.Finally before pushing the second view controller to the navigation stack you have to assign the sharedData object in your first view controller to sharedData in your second view controller.

in your FirstStepViewController.m add:

[varSecondViewController sharedData] = [self sharedData];

before:

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

Finally make sure you have connected all your outlets correctly in interface builder and everything should run perfectly then :)

Sepehr