2015-10-10 43 views
0

在我的HighscoreVC i顯示索引,名稱和分數。我有一個重置按鈕。當我按下按鈕時,名稱和分數等所有條目都必須默認。用名稱和分數重新加載高分

所以這裏是我的繪製代碼高分:右

-(void) viewDidLoad { 
[super viewDidLoad]; 


NSArray *highscore = [[[HighscoreModel alloc] init] getHighscore]; 
NSInteger i = 0; 

for (NSDictionary *scoreDict in highscore) { 

    if (i<=4) { 

    [self addScoreRowViewWithIndexLeft:(++i) andName:[scoreDict objectForKey:@"name"] andScore:[[scoreDict objectForKey:@"points"] integerValue]]; 

    } 

    else if (i>=5) { 

    [self addScoreRowViewWithIndexRight:(++i) andName:[scoreDict objectForKey:@"name"] andScore:[[scoreDict objectForKey:@"points"] integerValue]]; 

    } 

} 

} 

-(void) addScoreRowViewWithIndexLeft:(NSInteger)index andName: (NSString *)name andScore: (NSInteger)score { 


UIView *scoreRowView = [[UIView alloc] initWithFrame:CGRectMake(122, 113 + index * 80, 280, 20)]; 

UILabel *nameLabel  = [[UILabel alloc] initWithFrame:CGRectMake(100, 20, 180, 20)]; 

UILabel *scoreLabel = [[UILabel alloc] initWithFrame:CGRectMake(325, 20, 50, 20)]; 

UIColor *color = [UIColor colorWithRed:96.0f/255.0f green:96.0f/255.0f blue:96.0f/255.0f alpha:1.0]; 

[nameLabel setText:name]; 
[nameLabel setTextAlignment:NSTextAlignmentCenter]; 
[nameLabel setFont:[UIFont fontWithName:@"DINAlternate-Bold" size:18]]; 
[nameLabel setTextColor:color]; 

if (![name isEqual: @"DEFAULT"]) { 
    _resetButton.hidden = NO; 
} 


[scoreLabel setText:[@((int)score) stringValue]]; 
[scoreLabel setTextAlignment:NSTextAlignmentCenter]; 
[scoreLabel setFont:[UIFont fontWithName:@"DINAlternate-Bold" size:18]]; 
[scoreLabel setTextColor:color]; 


[scoreRowView addSubview:nameLabel]; 
[scoreRowView addSubview:scoreLabel]; 

[self.backgroundView addSubview:scoreRowView]; 


} 

相同的代碼。

,這是我的復位按鈕:

- (IBAction)yesButton:(id)sender { 
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; 
[defaults removeObjectForKey: @"highscore"]; 


NSArray *highscore = [[[HighscoreModel alloc] init] getHighscore]; 
NSInteger i = 0; 

for (NSDictionary *scoreDict in highscore) { 

    if (i<=4) { 

     [self addScoreRowViewWithIndexLeft:(++i) andName:[scoreDict objectForKey:@"name"] andScore:[[scoreDict objectForKey:@"points"] integerValue]]; 

    } 

    else if (i>=5) { 

     [self addScoreRowViewWithIndexRight:(++i) andName:[scoreDict objectForKey:@"name"] andScore:[[scoreDict objectForKey:@"points"] integerValue]]; 

    } 

} 

[self.backgroundView setNeedsDisplay]; 

} 

,但它不能正常工作。它將默認的高分放在舊的高分上。沒有刪除舊文本。如果我更改爲其他VC,回到我的高分,這是正確顯示......

回答

0

每次你打電話的addScoreRowViewWithIndexLeft,您要添加的看法,因此這兩條線:

[scoreRowView addSubview:nameLabel]; 
[scoreRowView addSubview:scoreLabel]; 
所以每次

你可以稱之爲addScoreRowViewWithIndexLeft的方法,當你將子視圖添加到VC的視圖時,視圖將一直保留,只要這個VC的視圖處於活動狀態,視圖就會相互疊加。

裏面(IBAction)yesButton:(id)sender所有你需要做的是更新的nameLabel文字和scoreLabel