Q:

how to give imageView tag in objective-c

I am developing an App for kids.
 images=[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg"],[UIImage imageNamed:@"Christmas-Wallpapers-HD-Picture.jpg"],[UIImage imageNamed:@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg"],[UIImage imageNamed:@"tree.jpg"],[UIImage imageNamed:@"luxury-christmas-napkins-father-christmas-1635-p.jpg"],[UIImage imageNamed:@"Navarre-Family-Eye-Care-Navarre-Florida-Optometrist-Santa-Christmas-Toy-Safety.jpg"],[UIImage imageNamed:@"Christmas-Wallpapers-HD-Picture.jpg"],[UIImage imageNamed:@"Christmas-Wallpaper-jesus-9413550-1024-768.jpg"],[UIImage imageNamed:@"tree.jpg"],[UIImage imageNamed:@"luxury-christmas-napkins-father-christmas-1635-p.jpg"], nil];

    NSString *dd=[NSString stringWithFormat:@"%@", images];
    NSLog(@"%@",dd);

    randomIndex1=arc4random() % images.count;
    selectedImage = [images objectAtIndex:randomIndex1]; //random selected image

    NSLog(@"%@",selectedImage);

    number = [[NSMutableArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10", nil];

    randomNumber = arc4random() % number.count; //random selected number
    NSLog(@"%d",randomNumber);

    for (int i = 0; i <randomNumber; i++)
    {
        NSLog(@"%@", selectedImage);

        // _img.image = selectedImage;

        //cell.img.image=selectedImage;

    }

i have used UIView .In UIView i placed 15 ImageViews .As i got the selectedImage from  the output.i need to place the image in ImageView based on tag.how to do 

A:

You need to set tag for all 15 images in view like 1,2,3,4,5...15
then check it like
for (int i = 0 ; i < randomNumber ; i++){
    id subView = [view viewWithTag:i];

    if ([subView isKindOfClass:[UIImageView class]]){
        ((UIImageView *)subView).image = selectedImage;
    }

    if ([subView isKindOfClass:[UIButton class]]){
       [((UIButton *)subView) setImage:selectedImage forState:UIControlStateNormal];
    }
}

say randomNumber = 5 , then you can set image for your first 5 ImageView.

