2013-03-12 115 views
0

在我的匹配紙牌遊戲中,我爲newGame創建了IBAction方法。無法更新UI

現在,我有一個名爲updateUI的方法來根據操作更改UI的狀態。

在我updateUI方法我有一個即時調用阿爾法消逝匹配的卡,但是當我打電話newGame方法,該卡仍然褪色從我叫阿爾法的時間了......

這是我的ViewController,請告訴我我做錯了什麼..謝謝! :

#import "CardGameViewController.h" 
#import "PlayingCardsDeck.h" 
#import "CardMatchingGame.h" 


@interface CardGameViewController() 

@property (weak, nonatomic) IBOutlet UILabel *flipsLabel; 
@property (weak, nonatomic) IBOutlet UILabel *notificationLabel; 
@property (weak, nonatomic) IBOutlet UILabel *scoreCounter; 
@property (strong, nonatomic) IBOutletCollection(UIButton) NSArray *cardButtons; 

@property (strong, nonatomic) CardMatchingGame *game; 
@property (nonatomic) int flipsCount; 

@end 


@implementation CardGameViewController 

//creating the getter method that creates a new card game. 
-(CardMatchingGame *) game { 

    if (!_game) _game = [[CardMatchingGame alloc] initWithCardCount:self.cardButtons.count usingDeck:[[PlayingCardsDeck alloc] init]]; 
    return _game; 
} 

//creating a setter for the IBOutletCollection cardButtons 
-(void) setCardButtons:(NSArray *)cardButtons { 

    _cardButtons = cardButtons; 
    [self updateUI]; 
} 


//creating the setter for the flipCount property. Whick is setting the flipsLabel to the right text and adding the number of counts. 
-(void) setFlipsCount:(int)flipsCount { 

    _flipsCount = flipsCount; 
    self.flipsLabel.text = [NSString stringWithFormat:@"Flips: %d", self.flipsCount]; 

} 


-(void) updateUI { 

    for (UIButton *cardButton in self.cardButtons) { 
     Card *card = [self.game cardAtIndex:[self.cardButtons indexOfObject:cardButton]]; 
     [cardButton setTitle:card.contents forState:UIControlStateSelected]; 
     [cardButton setTitle:card.contents forState:UIControlStateSelected|UIControlStateDisabled]; 
     cardButton.selected = card.isFaceUp; 
     cardButton.enabled = !card.unplayble; 
     if (card.unplayble) { 
      cardButton.alpha = 0.1; 
     } 
     self.scoreCounter.text = [NSString stringWithFormat:@"Score: %d", self.game.score]; 


     if (self.game.notification) { 

     self.notificationLabel.text = self.game.notification; 

     } 
    } 
} 


//Here I created a method to flipCards when the card is selected, and give the user a random card from the deck each time he flips the card. After each flip i'm incrementing the flipCount setter by one. 
- (IBAction)flipCard:(UIButton *)sender { 

    [self.game flipCardAtIndex:[self.cardButtons indexOfObject:sender]]; 
    self.flipsCount++; 
    [self updateUI]; 
} 



- (IBAction)newGame:(UIButton *)sender { 

    [self.game cleanHistory]; 

    self.flipsCount = 0; 

    self.game = nil; 

    [self updateUI]; 

} 


@end 

這是cleanHistory方法:

-(void)cleanHistory { 

    for (Card *card in self.cards) { 
     card.unplayble = NO; 
     card.faceUp = NO; 
    } 

    self.notification = nil; 
    self.score = 0; 

} 

回答

1

確保您擁有的所有對象存在並且沒有發佈/自動釋放和/或確保你在你的主線程刷新UI。

+0

我使用ARC ...你是什麼意思的主線程? @ arun.s – JohnBigs 2013-03-12 05:52:56

+1

Ya ... bt確保你的對象在那裏..例如:cardbuttons對象在你的數組中有沒有?主線程是所有UI刷新發生的地方。從二次Therad調用UI時不會刷新 – 2013-03-12 05:57:12