0

This is my first question on this forum and I hope u don't blame me. I'm trying to make an app and need some help with NSMutableArray.

I declared in .h file some UIIMageView objects like this:

IBOutlet UIImageView *image1;
IBOutlet UIImageView *image2;
NSMuttableArray *images;

In .m file i set up the Hidden:YES. Also I inserted them in an NSMuttableArray like this:

images = [[NSMutableArray alloc] initWithCapacity:2];
    [images addObject:image1];
    [images addObject:image2];
  1. How can I set up now a random UIImageView from the Array to be setHidden:NO?
  2. How to remove that object from the Array after it's set up as not hidden?
  3. After an action like -(IBAction) btnclick {} to make it again Hidden and show next random image from this NSMuttableArray in case they r much more.
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Why an array of UIImageView's? Use an array of UIImages instead so that you don't have to load tons and tons of UIImageViews on your screen and make it slow as hell – Totumus Maximus Aug 16 '12 at 01:07
  • How to do them please? Cause when i'm tryin' to declare in .h file IBOutlet UIImage, in Interface Builder i can't put those objects. Or should I put them programatically? – Sergiu Olaru Aug 18 '12 at 22:52

2 Answers2

1

This is probably what you want to do:

(also I'll make it an array of UIImages since that is the way you should present random images imo but then again you can do this with UIImageViews as well, which is again not recommended)

-(void)buttonClickedMethod
{
    if(images.count > 0)
    {
        int randomValue = arc4random_uniform(images.count); //get yourself a nice random value as used in http://stackoverflow.com/questions/160890/generating-random-numbers-in-objective-c
        myImageView.image = [images getObjectAtIndex:randomValue]; //get yourself the random image and set it to your UIImageView (which you probably want it to be image1 or image2 in your case)
        [images removeObjectAtIndex:randomValue]; //remove the random image from you list so that you want get it again the next you click the button
    }
}

This is probably the best way to deal with your problem.

You might want to add a method that removes the random image from the image view afterwards but then again you might not.

Totumus Maximus
  • 7,543
  • 6
  • 45
  • 69
0

One way I think you can try is the following:

  1. Assign a tag to each of your UIImageView. Say you have 100 UIImageViews, so you have tags from 1 - 100. This can be done via [view setTag:1];, something like that.

  2. You create an NSMutableSet that contains 1 - 100 (they need to be NSNumbers because NSSet only accepts objects).

  3. To retrieve an object from the set, you do [set anyObject]. You can then convert it back to integer and use viewWithTag: to get the view. Then you can setHidden:NO.

  4. Finally you remove the object from your NSMutableSet, easy stuff.

Array is by definition ordered. Set is much better for your purpose. Also I think it's kinda weird to add UIImageViews to array.

danqing
  • 3,348
  • 2
  • 27
  • 43
  • what do you mean by the SET command? Everything in my answer applies to iOS, and `NSSet` is available on iOS. – danqing Aug 18 '12 at 23:02
  • Sorry! When I searched on developer.apple.com it showed that's a command available to MacOS 10.4 and up. Now I found that also available for the iOS. Sorry again. – Sergiu Olaru Aug 19 '12 at 00:45