2012-02-27 21 views
1

我是編程的小老弟,但我開始學習。我正在爲iPhone寫作,現在我陷入了這個問題。目標C:從多個外部類訪問同一班級的數據

我一直在嘗試在stackowerflow上找到答案,但是很難將這些信息與我自己的項目相關聯。

正如標題所述,我試圖通過其他「外部」類來訪問一個類的數據。我習慣於Java,但我知道這不會以同樣的方式工作。

如何從類中的文本字段保存字符串,以便稍後從其他類中檢索該字符串?

我在這裏本3類:

一個「模型」級以保存字符串數據 一個「視圖 - 控制器」級以保存TextField的字符串中的「模式」級 一

#import <Foundation/Foundation.h> 

@interface Model : NSObject 

- (void)setPlayerOneName:(NSString *)tfString; 
- (NSString *)getPlayerOne; 

@end 

model.m:

「記分牌」 級從 「模型」 級

第一model.h提取數據

目前,class.h保存字符串中的 「型號」 級:

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 

@end 

.M:

#import "ViewController.h" 
#import "Model.h" 
#import "ScoreBoard.h" 

@interface ViewController() 

@property (strong) Model *model; 

@end 

@implementation Whist_CalculatorViewController 

- (void)textFieldDidEndEditing:(UITextField *)textField{ 
    [self.model setPlayerOneName:textField.text]; 
} 

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    if ([segue.identifier isEqualToString:@"saveAndStart"]){ 
     NSLog(@"prepareForSegue: %@", segue.identifier); 

     [segue.destinationViewController updateThatScoreBoard]; 
    } 
} 

@end 

最後記分牌類,至極訪問模型名稱字符串: ScoreBoard.h:

#import <UIKit/UIKit.h> 

@interface ScoreBoard : UIViewController 

@property (weak, nonatomic) IBOutlet UILabel *player1Label; 

- (void)updateThatScoreBoard; 

@end 

和ScoreBoard.m:

#import "ScoreBoard.h" 
#import "Model.h" 

@interface ScoreBoard() 

@property (strong) Model *model; 

@end 

@implementation ScoreBoard 

@synthesize player1Label = _player1Label; 
@synthesize model = _model; 

- (void)updateThatScoreBoard{ 
    [self.player1Label setText:[self.model getPlayerOne]]; 
} 

- (void)viewDidLoad{ 
    [self updateThatScoreBoard]; 
} 

- (void)viewDidUnload { 
    [self setPlayer1Label:nil]; 
    [super viewDidUnload]; 
} 
@end 

任何輸入是非常感謝!

回答

1

其實你應該把屬性定義放到你的.h文件中,因爲這是你要在其他類中包含的屬性。

這裏是我的版本,這一問題:

model.h

@interface Model : NSObject { 
@private 
    NSString *playerOne; 
} 
    // It is readonly, since you want this class use merely as a DTO 
    @property (readonly) NSString playerOne; 

    // Just give a static method that creates the DTO with some value 
    // to keep the actual Model object immutable! 
    +(id) createModelWithPlayer: (NSString*)p1; 
@end 

model.m

#import "Model.h" 

@implementation Model 
@synthesize playerOne; 

    #pragma mark - private accessors 
    -(void) setPlayerOne:(NSString*)pO { 
     playerOne = pO; 
    } 
    #pragma mark - 

    +(id) createModelWithPlayer: (NSString*)p1 { 
     Model *m = [[Model alloc] init]; 

     [m setPlayerOne: p1]; 
     return m; 
    } 
@end 

到目前爲止,我改變你的樣品。您可以通過訪問您的playerOne:

Model *m = [Model createModelWithPlayer:@"Rookey"]; 
... 
NSLog(@"Player one: %@", m.playerOne); 

更進一步,我不會這個模型的實例存儲在每個類別中,如果不是真的有必要,只是通過它它將被使用的方法。

另一種可能性是在整個應用程序中只有一個模型實例。在這種情況下,你的模型可以被編程如下(你可以使用Singleton模式):

model.h:

@interface Model : NSObject 
    // It is readonly, since you want this class use merely as a DTO 
    @property (assign) NSString playerOne; 

    // Just give a static method that creates the DTO with some value 
    // to keep the actual Model object immutable! 
    +(id) instance; 
@end 

model.m:

#import "Model.h" 

@implementation Model 
@synthesize playerOne; 

    #pragma mark - private accessors 
    -(void) setPlayerOne:(NSString*)pO { 
     playerOne = pO; 
    } 
    #pragma mark - 

    static Model *oneAndOnlyInstance; 
    +(id) instance { 
     if (oneAndOnlyInstance == nil) 
      oneAndOnlyInstance = [[Model alloc] init]; 

     return oneAndOnlyInstance; 
    } 
@end 

這樣就可以按如下方式訪問您的模型實例:

someclass.m:

#include "Model.h" 

//... 
[Model instance].playerOne = @"Rookey"; 
//... 

anotherClass.m:

#include "Model.h" 

//... 
NSLog(@"player one: %@", [M instance].playerOne); 
//... 

您可以在Wikipedia閱讀更多關於Singleton模式

+0

非常感謝您的回答:)我仍然不知道如何從「同一獲取數據「模型實例。如果我在一個類中創建模型並存儲數據,但是需要從另一個類中收集數據,然後再次創建模型 - 那麼它將成爲一個新的空模型? – NooberMan 2012-02-27 19:01:20

+0

我編輯了我的答案。我希望它對你有所幫助 – GeT 2012-02-27 19:17:36

+0

所有這些看起來都很棒,但有兩點:1,「#pragma mark - 」是什麼意思? 2,我的xcode似乎不喜歡「靜態模型oneAndOnlyInstance;」我不知道它做了什麼。但除此之外,這看起來像是一個偉大的解決我的問題 – NooberMan 2012-02-27 19:43:39