我正在制作一个骰子游戏。我有5个骰子。我将它们全部滚动以获得所有5个骰子的随机值。我添加了每个骰子,并在标签上打印出8“sumTotal”。
我想知道每次用户点击按钮时如何将每个值“sumTotal”存储到数组中。
我想知道一旦我打印出8号总数。如何删除它,并将下一个值添加到标签的开头。
谢谢
- (IBAction)rollDice:(id)sender {
self.die1Value = (arc4random() % 6) + 1;
self.die2Value = (arc4random() % 6) + 1;
self.die3Value = (arc4random() % 6) + 1;
self.die4Value = (arc4random() % 6) + 1;
self.die5Value = (arc4random() % 6) + 1;
[self.dieOne setTitle:[NSString stringWithFormat:@"%li",(long)self.die1Value ] forState:UIControlStateNormal];
[self.dieTwo setTitle:[NSString stringWithFormat:@"%li",(long)self.die2Value ] forState:UIControlStateNormal];
[self.dieThree setTitle:[NSString stringWithFormat:@"%li",(long)self.die3Value ] forState:UIControlStateNormal];
[self.dieFour setTitle:[NSString stringWithFormat:@"%li",(long)self.die4Value ] forState:UIControlStateNormal];
[self.dieFive setTitle:[NSString stringWithFormat:@"%li",(long)self.die5Value ] forState:UIControlStateNormal];
self.sumTotal = self.die1Value + self.die2Value + self.die3Value + self.die4Value + self.die5Value;
if (self.numberOfLines <= 8) {
// if (self.numberOfLines <= 8) {
NSString *i = [NSString stringWithFormat:@"\nTotal is: %d", self.sumTotal];
self.totalLabelIt.text = [self.totalLabelIt.text stringByAppendingString:i];
if (self.numberOfLines > 8) {
// if (self.numberOfLines <= 8) {
NSString *i = [NSString stringWithFormat:@"\nTotal issss: %d", self.sumTotal];
self.totalLabelIt.text = [self.totalLabelIt.text stringByAppendingString:i];
}
}
}
答案 0 :(得分:0)
您可以将总计存储在数组中。以下代码存储总计,在8个总计之后,再次从数组的开头开始。
@property (strong,nonatomic) NSMutableArray totals;
@property NSInteger arrayPointer;
-(void)viewDidLoad {
[super viewDidLoad];
self.totals=[NSMutableArray new];
self.arrayPointer=0;
}
- (IBAction)rollDice:(id)sender {
self.die1Value = (arc4random() % 6) + 1;
self.die2Value = (arc4random() % 6) + 1;
self.die3Value = (arc4random() % 6) + 1;
self.die4Value = (arc4random() % 6) + 1;
self.die5Value = (arc4random() % 6) + 1;
[self.dieOne setTitle:[NSString stringWithFormat:@"%li",(long)self.die1Value ] forState:UIControlStateNormal];
[self.dieTwo setTitle:[NSString stringWithFormat:@"%li",(long)self.die2Value ] forState:UIControlStateNormal];
[self.dieThree setTitle:[NSString stringWithFormat:@"%li",(long)self.die3Value ] forState:UIControlStateNormal];
[self.dieFour setTitle:[NSString stringWithFormat:@"%li",(long)self.die4Value ] forState:UIControlStateNormal];
[self.dieFive setTitle:[NSString stringWithFormat:@"%li",(long)self.die5Value ] forState:UIControlStateNormal];
NSInteger sumTotal = self.die1Value + self.die2Value + self.die3Value + self.die4Value + self.die5Value;
[self addTotalToArray:sumTotal];
if (self.numberOfLines <= 8) {
// if (self.numberOfLines <= 8) {
NSString *i = [NSString stringWithFormat:@"\nTotal is: %d", self.sumTotal];
self.totalLabelIt.text = [self.totalLabelIt.text stringByAppendingString:i];
if (self.numberOfLines > 8) {
// if (self.numberOfLines <= 8) {
NSString *i = [NSString stringWithFormat:@"\nTotal issss: %d", self.sumTotal];
self.totalLabelIt.text = [self.totalLabelIt.text stringByAppendingString:i];
}
}
-(void) addTotalToArray:(NSInteger) total {
NSNumber totalNum=[NSNumber numberWithInteger:total];
if (self.arrayPointer > self.totals.count-1) {
[self.totals addObject:totalNum];
} else {
self.totals[self.arrayPointer]=totalNum;
}
if (++self.arrayPointer == 8) {
self.arrayPointer=0;
}
}
正如我在评论中所说,我建议您使用数组来驱动UITableView
,而不是试图操纵标签