Objective C - 随机 XIB 选择

发布于 2024-11-19 15:26:10 字数 1156 浏览 1 评论 0原文

我已经发布了关于此的另一个问题,但似乎没有人知道如何做到这一点。

我希望我的应用程序为我选择一个随机 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃安静 2024-11-26 15:26:10
  • 为什么不创建一个可变数组
    并用所有人的名字填充它
    你的笔尖。
  • 然后读取数组的计数并
    生成一个随机数
    范围。
  • 提取该索引处的笔尖名称
    并将其从数组中删除。
  • 重复步骤 2-3。
//在适当的位置设置你的列表 

NSMutableArray *nibs
= [[NSMutableArray alloc] initWithObjects: @"一个笔尖", @"另一个
笔尖”,@“最后一个笔尖”,nil];

self.unusedNibs = 笔尖; //这应该是您声明的属性
你的标题。

[笔尖释放];

-(IBAction)ContinueAction:(id)sender{

int random = arc4random() % [self.unusedNibs count]; 

NSString
*nibName = [self.unusedNibs objectAtIndex: random];

[self.unusedNibsremoveObjectAtIndex:
随机];

//在此加载笔尖。

}
  • Why don't you make a mutable array
    and populate it with the names of all
    your nibs.
  • Then read the count of the array and
    generate a random number in that
    range.
  • Extract the nib name at that index
    and remove it from the array.
  • repeat steps 2-3.
//Setup your list at an appropriate place 

NSMutableArray *nibs
= [[NSMutableArray alloc] initWithObjects: @"One Nib", @"Another
Nib", @"Last Nib", nil];

self.unusedNibs = nibs; //This should be a property you declare in
your header.

[nibs release];

-(IBAction)continueAction:(id)sender{

int random = arc4random() % [self.unusedNibs count]; 

NSString
*nibName = [self.unusedNibs objectAtIndex: random];

[self.unusedNibs removeObjectAtIndex:
random];

//Load nib here.

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文