【发布时间】:2010-05-11 15:13:19
【问题描述】:
我正在制作一个小测验式的应用程序,但我遇到了一些问题。
我使用 arc4random() 从 NSMutableArray 中随机抽取问题,然后使用 3 个按钮填充视图,其中一个包含正确答案,另外 2 个包含两个错误答案
我需要做的是随机化视图中 3 个按钮的 X 坐标(位置)这是我正在使用的代码,但它给我带来了问题,因为它不能正常工作并且应用程序经常崩溃调用动作时:
NSBundle *bundle02 = [NSBundle mainBundle];
NSString *textFilePath02 = [bundle02 pathForResource:@"possiblePositions" ofType:@"txt"];
NSString *fileContents02 = [NSString stringWithContentsOfFile:textFilePath02 encoding:NSUTF8StringEncoding error:nil];
arrayPossiblePositions = [[NSMutableArray alloc] initWithArray:[fileContents02 componentsSeparatedByString:@"\n"]];
int length02 = [arrayPossiblePositions count];
int chosen02 = arc4random() % length02;
[arrayPossiblePositions removeObjectAtIndex:chosen02];
int chosen04 = arc4random() % length02;
[arrayPossiblePositions removeObjectAtIndex:chosen04];
int chosen05 = arc4random() % length02;
if ([questionString isEqualToString:@"question1"]) {
buttonCorrect = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize = CGRectMake(chosen02, 80, 130, 130);
buttonCorrect.frame = newSize;
[buttonCorrect setImage:[UIImage imageNamed:@"kncpf.png"] forState:UIControlStateNormal];
[buttonCorrect addTarget:self action:@selector(answerCorrect) forControlEvents:UIControlEventTouchUpInside];
[main addSubview:buttonCorrect];
buttonUncorrect01 = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize02 = CGRectMake(chosen04, 80, 130, 130);
buttonUncorrect01.frame = newSize02;
[buttonUncorrect01 setImage:[UIImage imageNamed:@"kncpf02.png"] forState:UIControlStateNormal];
[buttonUncorrect01 addTarget:self action:@selector(answerUncorrect) forControlEvents:UIControlEventTouchUpInside];
[main addSubview:buttonUncorrect01];
buttonUncorrect02 = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize034578 = CGRectMake(chosen05, 80, 130, 130);
buttonUncorrect02.frame = newSize034578;
[buttonUncorrect02 setImage:[UIImage imageNamed:@"kncpf034578.png"] forState:UIControlStateNormal];
[buttonUncorrect02 addTarget:self action:@selector(answerUncorrect) forControlEvents:UIControlEventTouchUpInside];
[main addSubview:buttonUncorrect02];
}
你能建议我做一些不同的事情吗,因为我真的快疯了?
提前感谢您的回答, 大卫
【问题讨论】:
标签: iphone objective-c random position