「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
。
これができるかどうかのアイデアはありますか?