2012-10-30 127 views
1

我剛開始使用Objective C,我想知道選擇所有其他球員將isActive屬性設置爲NO的正確語法。謝謝!選擇所有球員對象ObjectiveC

- (void) playerBuzzedIn: (XGPlayer *) activePlayer 
{ 
    if (activePlayer) { 
     activePlayer.isActive = YES; 
    } 
    else 
     //set all other isActive = NO 
    self.currentGameState = results; 
} 

林想這樣的,是正確的:

for (XGPlayer *player in self.players) { 
player.isActive = NO; 
} 
+0

如果在實現'-playerBuzzedIn:'的類中有'players' @ property',它是'XGPlayer'對象的'NSArray',那麼,是的,您建議的代碼是正確的。你試過了嗎? –

+0

是的,它的作品,謝謝! – JProg

回答

0

首先你需要的所有球員名單;讓我們假設它是這樣的:

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; 
}