1

「playerOne」「playerTwo」などの接頭辞が付いたIBOutletがいくつかあります。

@property (weak, nonatomic) IBOutlet UIButton *playerOneTurnButton;
@property (weak, nonatomic) IBOutlet UIButton *playerTwoTurnButton;
@property (weak, nonatomic) IBOutlet UIButton *playerOneChallengeButton;
@property (weak, nonatomic) IBOutlet UIButton *playerTwoChallengeButton;
@property (weak, nonatomic) IBOutlet UILabel *playerOneTimeLabel;
@property (weak, nonatomic) IBOutlet UILabel *playerTwoTimeLabel;

そして、「playerOne」「playerTwo」などの接頭辞が付いたいくつかの変数を宣言しました...

int playerOneTimeRemaining;
int playerTwoTimeRemaining;
int playerThreeTimeRemaining;

etc ...

さまざまな時点で、誰の番であるかに応じてこれらのアウトレットを操作 (アルファ、非表示、およびアクティブ プロパティを調整) したいと思います。また、いくつかの変数の int 値を変更する必要もあります。私は、誰が行くのかを追跡するさまざまなNSStrings(この例では と呼ばれるもの) を持っています。などなど。whoWasChallengedFromGame@"playerOne"@playerTwo"

現時点では、文字列をチェックしてコード ブロックを実行する if ステートメントがあります。このコードのブロックは、アウトレットと変数の名前が毎回調整されるだけで、毎回ほぼ同じです...

-(void)challengeAccepted {
Store *myStore = [Store sharedStore];
if (([myStore.whoWasChallengedFromGame isEqual:@"playerOne"]) && ([myStore.brutalModeFromOptions isEqual:@"lenient"])) {
    playerOneTimeRemaining -= 30;
    playerOneTimeLabel.text = [NSString stringWithFormat:@"%i", playerOneTimeRemaining];
}

if (([myStore.whoWasChallengedFromGame isEqual:@"playerTwo"]) && ([myStore.brutalModeFromOptions isEqual:@"lenient"])) {
    playerTwoTimeRemaining -= 30;
    playerTwoTimeLabel.text = [NSString stringWithFormat:@"%i", playerTwoTimeRemaining];

}

etc etc for each player

ご覧のとおり、プレーヤーの数が増えるにつれて、これは実際には扱いにくくなります。私が探しているのは、文字列をチェックし、変数名whoWasChallengedFromGameの前に付ける単一のコードブロックを記述できるようにすることですIBOutlet

したがって、コードは単に言う必要があります

"string"TimeRemaining -=30
"string"TimeLabel.text= blah blah

コードは単に「string」部分を playerOne または playerTwo または string にあるものに変換しますwhoWasChallengedFromGame

これができるかどうかのアイデアはありますか?

4

2 に答える 2

0

PlayerControlsクラスを作成viewDidLoadし、ゲーム内の for each player で初期化してから、for PlayerControlscurrent player を使用して調整を行います。

簡単な例を次に示します。

@interface PlayerControls : NSObject
@property (weak, nonatomic) UIButton *turnButton;
@property (weak, nonatomic) UIButton *challengeButton;
@property (weak, nonatomic) UILabel *timeLabel;
@end

のプレーヤーごとのインスタンスを作成PlayerControlsし、それらを配列に入れます。この宣言をコントローラーに追加します。

PlayerControls *playerControls[2];

-(void)viewDidLoad {
    playerControls[0] = [[PlayerControls alloc] init];
    playerControls[0].turnButton = self.playerOneTurnButton;
    playerControls[0].challengeButton = self.playerOneChallengeButton;
    playerControls[0].timeLabel = self.playerOneTimeLabel;
    playerControls[1] = [[PlayerControls alloc] init];
    playerControls[1].turnButton = self.playerTwoTurnButton;
    playerControls[1].challengeButton = self.playerTwoChallengeButton;
    playerControls[1].timeLabel = self.playerTwoTimeLabel;
}

この時点から、現在のプレーヤーをインデックスで参照できます。プレーヤー 1 の場合は 0、プレーヤー 2 の場合は 1 です。コードを変更せずに、必要に応じてプレーヤーを追加できます。

于 2013-02-22T01:36:38.513 に答える
0

Key-Value Coding (KVC)を使用すると、目的を達成することができます。

特に、こちらのページをご覧ください。プレーヤー情報を組み込むには、次のようにします。

[myInstance setValue:someAppropriateValue
              forKey:[NSString stringWithFormat:@"%@challengeButton", currentPlayer]];

ただし、おそらくクラスを使用してこれを行うでしょう。ただし、ラッパー クラスを使用してプレーヤーを管理することを強くお勧めします。

@interface PlayerStates

- (UIButton *)turnButtonForPlayer:(NSInteger)player
...etc

@end

このアプローチの欠点は、IBOutlets を直接使用できないことです (ただし、 から構築することはできますIBOutletCollection)。

IBOutletウィジェットごとに使用する必要がある場合は、おそらく KVC を使用しますが、プレーヤーには必ず名前付き定数を使用してください。

typedef enum {
    PlayerOne,
    PlayerTwo,
} Player;

@property (nonatomic) Player currentPlayer;

そして、次を使用して KVC に必要な文字列を取得します。

- (NSString *)stringForPlayer:(Player)player
{
    switch (player) {
        case PlayerOne:
            return @"playerOne";
        ...
}
于 2013-02-22T02:23:41.613 に答える