目标 C:带有 NSArray 的 UIButton 图像以随机顺序 [英] Objective C : UIButton image with NSArray in a random order

查看:31
本文介绍了目标 C:带有 NSArray 的 UIButton 图像以随机顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于流程的简要想法:

我已经说过我的数据库中有最少 1 条记录和最多 18 条记录,其中包含一个文本和一个图像,我必须从中获取图像并将其中的任意 3 个随机放置到 3 个 UIButtons 中,其中一个图像应该符合我的问题.即 1 是答案,其他 2 是干扰项.

I have say minimum 1 and maximum 18 records in my database with a text and an image from which I have to fetch images and place any 3 of the same randomly to 3 UIButtons among which one image should match with my question. i.e. 1 is the answer and other 2 are the distractors.

例如:问题是:苹果在哪里?现在在一堆图像中,我想以随机顺序将任何 3 个图像放在 3 个 UIButton 上(1 个答案,即一个 Apple 和 2 个干扰项),并且在选择 UIButton 时,它应该提示我它是否是正确答案.

For Example : Question is : where is Apple? Now among the bunch of images, I want to place any 3 images on the 3 UIButtons(1 answer i.e. An Apple and 2 distractors) in a random order and on selecting the UIButton it should prompt me if it's a right answer or not.

我正在实现以下代码以随机顺序放置 UIImages :

I am implementing below code for placing the UIImages in a random order :

-(void) placeImages 
{
    NSMutableArray *images = [NSMutableArray arrayWithArray:arrImg];

    NSArray *buttons = [NSArray arrayWithObjects:btn1,btn2,btn3, nil];
    for (UIButton *btn in buttons)
    {
        int randomIndex= random() % images.count;
        UIImage *img = [images objectAtIndex:randomIndex];
        [btn setImage:img forState:UIControlStateNormal];
        [images removeObjectAtIndex:randomIndex];                        
    }
}

但是我被困在一个点上,我应该如何获得 1 个 UIButton 图像和答案以及其他作为干扰项,我应该如何维护 NSArray<中图像的索引/代码>?请指导我.谢谢.

But I am stuck at a point that how should I get 1 UIButton image with an answer and other as distractors also that how should i maintain the index of the image from the NSArray? Kindly guide me. Thank you.

推荐答案

将你的第一个按钮设为 Apple(正确答案)将其余按钮设置为随机错误答案,然后在您的应用中随机放置按钮

set your first button to be the Apple (correct answer) set the remaining buttons to be random wrong answers, then position the buttons randomly in your app

这篇关于目标 C:带有 NSArray 的 UIButton 图像以随机顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆