2012-02-27 9 views
1

私はプログラミング時にはうんざりですが、私は学び始めています。私はiPhoneのために書いています、そして、私は今この問題で立ち往生しています。目的C:複数の外部クラスから同じクラスのデータにアクセスする

私はここで答えを見つけようとしていますが、自分のプロジェクトに情報を関連付けるのは苦労しました。

タイトルには、あるクラスから他の「外部」クラスを介してデータにアクセスしようとしています。私はJavaに慣れていますが、これは同じ方法では動作しません。

クラス内のテキストフィールドから文字列を保存するにはどうすればいいですか?後でほかのクラスから取得するにはどうすればよいですか?

私にここに存在3クラス:

一つ-class「モデル」でテキストフィールド文字列を保存するために ワン「ビュー・コントローラ」級の文字列データを保存するための一つの「モデル」級

#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); 

本当に必要でないならば、私はちょうどそれが使用されるメソッドのためにそれを渡し、すべてのクラスでは、このモデルのインスタンスを格納しません。

もう1つの可能性は、アプリケーション全体でモデルのインスタンスを1つだけ持つことです。この場合、以下のようにあなたのモデルは、(あなたが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

+0

でシングルトンパターンの詳細を読むことができるおかげでそんなにあなたの答えのために:)私はまだ「同じからデータを取得する方法がわかりません"モデル - インスタンス。 1つのクラスにモデルを作成してデータを保存した後、別のクラスのデータを収集してモデルを作成する必要がある場合は、新しい空のモデルではありませんか? – NooberMan

+0

私は自分の答えを編集しました。私はそれが少しあなたを助けることを願っています – GeT

+0

これはすごく見えるが、2つのこと:1、 "#プラグママーク - "意味ですか? 2、私のxcodeは静的なモデルoneAndOnlyInstance;が好きではないようです私はそれが何をしているのか分からない。しかし、それ以外は、これは私の問題の大きな修正のように見えます – NooberMan

関連する問題