Objective C - 随机 XIB 选择
我已经发布了关于此的另一个问题,但似乎没有人知道如何做到这一点。
我希望我的应用程序为我选择一个随机 XIB 文件,但不要使用已经随机选择的文件。
这就是我现在所设置的,它似乎有效,但我必须一遍又一遍地按下按钮,直到找到一个尚未使用的按钮。
-(IBAction)continueAction:(id)sender{
random = arc4random() % 2;
if (random == 0 && usedQ2 == 0) {
Question_2 *Q2 = [[Question_2 alloc] initWithNibName:@"Question 2" bundle:nil];
Q2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:Q2 animated:YES];
[Q2 release];
}
else if (random == 1 && usedQ3 == 0) {
Question_3 *Q3 = [[Question_3 alloc] initWithNibName:@"Question 3" bundle:nil];
Q3.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:Q3 animated:YES];
[Q3 release];
}
}
正如你所看到的,我让它从一个随机数中选择,并从它们中找到它匹配的那个。
然后你可以看到我的 if 语句的另一部分正在检查以确保它以前没有被使用过。
每个NIB文件都有自己的usedQ(无论它是什么),当加载该Nib文件时,它会将usedQ设置为1。
我想我可以这样做,但为了摆脱不断按下按钮的情况,我将不得不放置大量的 else 语句,其中包含更多的 else 语句。
我也尝试过运行
random = arc4random() % 2;
in a while 语句和 for 语句,我希望它会继续寻找一个数字,直到找到一个没有使用过的数字,但运气不佳。
有什么帮助吗?谢谢!
I have already posted another question about this, but no one seemed to know how to do this.
I want my app to pick a random XIB file for me, but dont use the ones that have already been randomly picked.
So heres what i have set up as of right now, it seems to work, but i have to keep pressing the button over and over until it finds one that hasnt be used.
-(IBAction)continueAction:(id)sender{
random = arc4random() % 2;
if (random == 0 && usedQ2 == 0) {
Question_2 *Q2 = [[Question_2 alloc] initWithNibName:@"Question 2" bundle:nil];
Q2.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:Q2 animated:YES];
[Q2 release];
}
else if (random == 1 && usedQ3 == 0) {
Question_3 *Q3 = [[Question_3 alloc] initWithNibName:@"Question 3" bundle:nil];
Q3.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:Q3 animated:YES];
[Q3 release];
}
}
So as you can see i have it pick from a random number, and from their find the one that it matches.
Then you can see i have another part of my if statement that is checking to make sure it hasn't been used before.
each NIB file has its own usedQ(whatever Q it is), and when that Nib file is loaded it puts that usedQ as 1.
I think i could get by doing this, but in order to get rid of the constant button pushing, i will have to put loads of else statements with more else statements in them.
I have also tried running the
random = arc4random() % 2;
in a while statement and a for statement, i hoped that it would keep looking for a number until one that hasn't be used was found with no luck.
Any help? thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
并用所有人的名字填充它
你的笔尖。
生成一个随机数
范围。
并将其从数组中删除。
and populate it with the names of all
your nibs.
generate a random number in that
range.
and remove it from the array.