我的应用程序当前正在生成随机数(请参阅下面的代码)。我想要的是在用户点击“保存”按钮后保存该数字并将其显示在表格 View 上。
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self.clickyButton setTitle:@"Generate" forState:UIControlStateNormal];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)handleButtonClick:(id)sender {
// Generates numbers 1 to 49, without creating duplicate numbers.
NSMutableSet * numberSet = [NSMutableSet setWithCapacity:5];
while ([numberSet count] < 7 ) {
NSNumber * randomNumber = [NSNumber numberWithInt:(arc4random() % 49 + 1)];
[numberSet addObject:randomNumber];
}
NSArray * numbers = [numberSet allObjects];
self.n1.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:0]];
self.n2.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:2]];
self.n3.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:3]];
self.n4.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:4]];
self.n5.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:5]];
self.n6.text = [NSString stringWithFormat:@"%@", [numbers objectAtIndex:6]];
}
@end
请解释一下如何将其保存在表格 View 中。 xcode初学者。
最佳答案
您应该创建一个在整个类范围内可访问的变量,而不仅仅是特定的 -handleButtonClick:
方法,然后将生成的数字添加到该变量 - 数组、集合等...
从那里,您可以实现 TableView 以通过 var[indexPath.row]
从变量读取值(假设它是一个数组),并显示它。一旦数组填充了对象,您将需要调用 [tableView reloadData];
以确保 tableview 显示数据。
关于ios - 保存在 TableView 中生成的随机数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23326280/