我遵循James Brannan教程,並試圖在一些xbis之間共享數據。沒有運氣。在XIB之間共享數據
我有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點太具體了。插座可用於任何可存儲在筆尖中的物體。這些是最常見的視圖,包括但不限於控件,但可以使用任何實現NSCoding的對象。一個真實世界的例子是視圖控制器,它通常存儲在nib文件中。 – Caleb 2011-03-30 19:04:33