我正在制作一个小问答式应用程序,但我遇到了一些问题。
我使用arc4random()从NSMutableArray中随机输出问题,然后用3个按钮填充视图,其中一个包含正确答案,另一个包含两个错误答案
我需要做的是随机化视图中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];
}
你可以建议我做些不同的事情,因为我真的疯了吗?
提前感谢您的回答, 大卫
答案 0 :(得分:1)
我实际上需要做类似的事情,但不是移动图像,而是决定这样做:
- (void)checkIfCorrect:(id)sender {UIImage * buttonImage = sender.image;
if(buttonImage == [UIImage imageNamed:@“kncpf.png”]){[self answerCorrect]; } else {[self answerIncorrect]; }
}
编辑包含我推荐的代码:
此外,您正在致电
int length02 = [arrayPossiblePositions count];
int chosen02 = arc4random() % length02;
[arrayPossiblePositions removeObjectAtIndex:chosen02];
int chosen04 = arc4random() % length02;
[arrayPossiblePositions removeObjectAtIndex:chosen04];
int chosen05 = arc4random() % length02;
请注意,length02保持不变,而arrayPossiblePositions的大小会发生变化。这可能是您的代码崩溃的第一个原因:您正试图从数组中删除索引,而不是数组计数!
我没有测试,但应该工作:(不要忘记定义上面提到的checkanswer())
NSMutableArray *imagesArray = [NSMutableArray arrayWithObjects:@"image1.png", @"image2.png", @"image3.png", nil];
int count1 = [imagesArray count];
int index1 = arc4random() % count1;
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize = CGRectMake(30, 80, 130, 130);
button1.frame = newSize;
[button1 setImage:[UIImage imageNamed:[imagesArray objectAtIndex:index1]] forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(checkAnswer:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
[imagesArray removeObjectAtIndex:index1];
int count2 = [imagesArray count];
int index2 = arc4random() % count2;
button2 = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize = CGRectMake(160, 80, 130, 130);
button2.frame = newSize;
[button2 setImage:[UIImage imageNamed:[imagesArray objectAtIndex:index2]] forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(checkAnswer:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
[imagesArray removeObjectAtIndex:index2];
int count3 = [imagesArray count];
int index3 = arc4random() % count3;
button3 = [UIButton buttonWithType:UIButtonTypeCustom];
CGRect newSize = CGRectMake(290, 80, 130, 130);
button3.frame = newSize;
[button3 setImage:[UIImage imageNamed:[imagesArray objectAtIndex:index3]] forState:UIControlStateNormal];
[button3 addTarget:self action:@selector(checkAnswer:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button3];
答案 1 :(得分:1)
我认为arc4random()需要一个上限。不确定模数使用在这里是否正确或更可能是语义错误,不确定当您尝试执行类似操作时弧如何反应而不首先设置它的上限。我不确定,尝试用硬值代替你的长度02来看看你是否得到预期的行为,然后从那里向后工作。