首先你需要的所有球員名單;讓我們假設它是這樣的:
NSMutableArray *_allplayers = ...;
然後在你的方法,你可以設置所有玩家從誰剛剛發出嗡嗡聲的一種非活性分開:
- (void) playerBuzzedIn: (XGPlayer *) activePlayer
{
if (activePlayer)
{
for (XGPlayer *player in _allPlayers)
{
player.isActive = activePlayer == player; // Or perhaps [activePlayer isEqual:player] ?
}
}
self.currentGameState = results;
}
另一種方法是一個指針保持當前活動玩家:
在.h文件中:
@interface Whatever : NSObject
{
XGPlayer *_activePlayer = nil;
}
@property (retain, nonatomic, readonly) XGPlayer *activePlayer;
@end
在.m文件:
// Private Methods
@interface Whatever()
- (void)setActivePlayer:(XGPlayer *)activePlayer
@end
@implementation Whatever
@synthesize activePlayer = _activePlayer;
// Private setter method
- (void)setActivePlayer:(XGPlayer *)activePlayer
{
[activePlayer retain]; // If not using ARC
[_activePlayer release]; // If not using ARC
_activePlayer = activePlayer;
}
- (void) playerBuzzedIn: (XGPlayer *) activePlayer
{
self.activePlayer.isActive = NO;
activePlayer.isActive = YES;
self.activePlayer = activePlayer;
self.currentGameState = results;
}
如果在實現'-playerBuzzedIn:'的類中有'players' @ property',它是'XGPlayer'對象的'NSArray',那麼,是的,您建議的代碼是正確的。你試過了嗎? –
是的,它的作品,謝謝! – JProg