2012-04-12 5 views
1

私はこの問題に少し悩まされています!私は間違いなく多くの投稿を見てきましたが、それを動作させることはできません。以前は私のアプリケーションでは、プロパティに割り当てることによってクラス間でデータを渡すことができました。私はどこに間違っているのかわからないので、どんな助けもありがとう。私は次のコードが偉大ではないことを知っています、そして私は辞書に助けを求めていますが、今のところ私はこの作業をしたいと思います。整数をモデルクラスに渡す方法

- (IBAction)checkAnswers:(id)sender 
{ 
// The following if statements check user input and checks to see if it is the right answer. 
// The .text parts are uitextfield properties and then shows an image of a tick if correct. 
// It then assigns one to a variable and sums them up at the end (blag)  
if ([eyepiece.text isEqualToString:@"Eyepiece"]) { 
    UIImage *image = [UIImage imageNamed:@"Tick.png"]; 
    [eyepieceTick setImage:image]; 
    a = 1; 
} 

if ([focussingKnobs.text isEqualToString:@"Focussing knobs"]) { 
    UIImage *image = [UIImage imageNamed:@"Tick.png"]; 
    [focussingTick setImage:image]; 

    b = 1; 
} 

if ([objectiveLens.text isEqualToString:@"Objective lenses"]) { 
    UIImage *image = [UIImage imageNamed:@"Tick.png"]; 
    [objectiveTick setImage:image]; 
    c = 1; 
} 

if ([stage.text isEqualToString:@"Stage"]) { 
    UIImage *image = [UIImage imageNamed:@"Tick.png"]; 
    [stageTick setImage:image]; 
    d = 1; 
} 
if ([mirror.text isEqualToString:@"Mirror"]) { 
    UIImage *image = [UIImage imageNamed:@"Tick.png"]; 
    [mirrorTick setImage:image]; 
    e = 1; 
} 

blag = a + b + c + d + e; 

// Here I update a label with the score  

finalScore = [[NSString alloc] initWithFormat:@"%D", blag]; 
[score setText:finalScore]; 

// This is probably where I'm going wrong. I'm allocating a model class called 
// Level_3_Brain and trying to assign a new property in that class (cellsLevelThree) 
// with the score. 

// Level_3_Brain *level = [[Level_3_Brain alloc] init]; 
// level.cellsLevelThree = blag; 

// Updated to 
    [Level_3_Brain sharedInstanceOfLevel3].cellsLevelThree = blag; 


// I then set them all back to zero so that the score doesn't go above 5 
a = 0, b = 0, c = 0, d = 0, e = 0; 
blag = 0; 

} 

私のモデルクラスの.hは今持っている:

@interface Level_3_Brain : NSObject 

+ (id)sharedInstanceOfLevel3; 

@property (nonatomic) int cellsLevelThree; 

@end 

私の.mは今シングルトンの記事から取ったこのコードを持っています

@implementation Level_3_Brain 
@synthesize cellsLevelThree; 

static Level_3_Brain *sharedInstanceOfLevel3 = nil; 

// Get the shared instance and create it if necessary. 
+ (Level_3_Brain *)sharedInstanceOfLevel3 { 
if (sharedInstanceOfLevel3 == nil) 
{ 
sharedInstanceOfLevel3 = [[super allocWithZone:NULL] init]; 
} 



return sharedInstanceOfLevel3; 


} 

// We can still have a regular init method, that will get called the first time the Singleton is used. 

- (id)init 
{ 
self = [super init]; 

if (self) { 
    // Work your initialising magic here as you normally would 
} 

NSLog(@"%@", cellsLevelThree); 

return self; 
} 

// Your dealloc method will never be called, as the singleton survives for the duration of your app. 
// However, I like to include it so I know what memory I'm using (and incase, one day, I convert away from Singleton). 
-(void)dealloc 
{ 
// I'm never called! 
// [super dealloc]; 
} 

/We don't want to allocate a new instance, so return the current one. 
+ (id)allocWithZone:(NSZone*)zone { 
return [self sharedInstanceOfLevel3]; 
} 

// Equally, we don't want to generate multiple copies of the singleton. 
- (id)copyWithZone:(NSZone *)zone { 
return self; 
} 


@end 

残念ながら、私は今取得していますエラー "プロパティ 'cellsLevelThree'がタイプIDのオブジェクトに見つかりませんでした。

+0

最新の問題に対処するには、適切なタイプ '+(Level_3_Brain)sharedInstanceOfLevel3;を.hファイルに宣言するか、角括弧構文を使用して' cellsLevelThree'プロパティをドット構文の代わりに使用します。 – dasblinkenlight

+0

完了、少なくとも今実行中です! :)コンソールで0が返され、戻り値が押されるたびに更新されません... –

+0

'score'テキストは正しい値に更新されますか? – dasblinkenlight

答えて

1

あなたはwここでは、あなたのコメントで "間違っている":問題は、値を間違って設定しているのではなく、メソッドにローカルなまったく新しいインスタンスに設定していて、すぐに破棄されます。そのメソッドを終了します。

一般に、アプリケーションの起動時にモデルクラスを作成し、アプリケーションの存続期間中は使用可能な状態にしておく必要があります。これは多くの場合、singletonsを使用して実行されます。

+0

ご協力いただきありがとうございます。大変感謝しています。私は十分な評判(悲しいことに私が知っている)がないので、まだ私はupvoteできませんが、できるだけ早く私は:)。 –

+0

シングルトンの記事を読んで(興味深い)、私はちょっと後でもっと複雑なことをする予定の私のアプリでちょっとお手伝いできると思う。現時点では、私はそれを地元ではない別の場所に設定する必要がありますか?または、新しいメソッドを作成します。 –

+1

@RobWクラス内に共有インスタンスを提供することによって、既存のクラスをシングルトンにすることができます。たとえば、 'Level_3_Brain'をシングルトンにすると、モデルのヘッダをインクルードしたアプリケーションのどこからでも[Level_3_Brain instance] .cellsLevelThree = blag'と書くことができ、値は共有されたままになります。 – dasblinkenlight

関連する問題