我有许多 IBOutlets 前缀为“playerOne”“playerTwo”等。
@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 ...
在不同的点上,我想根据轮到谁来操纵这些出口(调整 alpha、隐藏和活动属性),我还需要更改一些变量的 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
并且代码将简单地将“字符串”部分转换为 playerOne 或 playerTwo 或字符串中的任何内容whoWasChallengedFromGame
。
如果可以做到这一点,有什么想法吗?