在单击 UIbutton 时将值输入到数组中
inputting values into an array as UIbutton is being clicked
我正在构建一个骰子游戏。我有 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];
}
}
}
您可以将总计存储在数组中。以下代码存储总计,在 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
而不是试图操纵标签
我正在构建一个骰子游戏。我有 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];
}
}
}
您可以将总计存储在数组中。以下代码存储总计,在 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
而不是试图操纵标签