Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

creating a previous button from an array of images

have a view and you're setting the view to contain a randomly selected image when the user presses one button, and you want the view to go back to the previous image when they press the other button

//this button displays images in the array randomly in no order
-(IBAction)nextbutton:(id)sender;
{
int ptr = rand() % 6;
NSArray *images = [[NSArray alloc] initWithObjects:@"Americans.png",@"Approach.png",@"Arianny.png",@"Atoms.png",@"Australia.png",@"Average.png",nil];

}

//I want to make a button that will display the previous image of the images in array above
Post edited by chris Good on

Replies

  • CiECiE Posts: 36Registered Users
    chris Good;400080 said:
    have a view and you're setting the view to contain a randomly selected image when the user presses one button, and you want the view to go back to the previous image when they press the other button

    //this button displays images in the array randomly in no order
    -(IBAction)nextbutton:(id)sender;
    {
    int ptr = rand() % 6;
    NSArray *images = [[NSArray alloc] initWithObjects:@"Americans.png",@"Approach.png",@"Arianny.png",@"Atoms.png",@"Australia.png",@"Average.png",nil];

    }

    //I want to make a button that will display the previous image of the images in array above
    So why don't you create another array that adds the numbers from the randomly generated selections. Then create the back button to pull from that array in the reverse order.
  • chris Goodchris Good Posts: 8New Users
    what code can I use to do this for a new button
  • baja_yubaja_yu Posts: 6,166Super Moderators, Registered Users
    Also, a few points in that code make no sense.


    int ptr=0;

    if (ptr <=5) {


    Why are you doing that?! You create ptr on the previous line and set its value to 0, what's the point of checking if it is less or equal to 5, it will always be true because it'll always be 0.


    ptr = 5;
    ptr--;


    Why?? Why set the value to 5 then decrement it by 1. Why not just: ptr = 4;
  • RhadeRhade Posts: 560Super Moderators
    Stop making multiple threads for the same topic. And we don't take kindly to multiple accounts around here. If it happens again, you're gone.
    Do not quote questionable posts.

    Do not post moderator requests in public. Send a PM.

    vvvvv----- Use the flag button to report spam.
  • Duncan CDuncan C Posts: 8,021Tutorial Authors, Registered Users
    chris Good;400080 said:
    have a view and you're setting the view to contain a randomly selected image when the user presses one button, and you want the view to go back to the previous image when they press the other button

    //this button displays images in the array randomly in no order
    -(IBAction)nextbutton:(id)sender;
    {
    int ptr = rand() % 6;
    NSArray *images = [[NSArray alloc] initWithObjects:@"Americans.png",@"Approach.png",@"Arianny.png",@"Atoms.png",@"Australia.png",@"Average.png",nil];

    }

    //I want to make a button that will display the previous image of the images in array above


    The code you posted does not load an image, or install it into a view. It doesn't even select a random image name from your array.

    Do you have more complete code for what you're trying to do, or is this as far as you've gotten?

    What you need to do is this:

    Create a new NSMutableArray as an instance variable, and initialize it to an empty array in your viewDidLoad method. Lets call it previousImageIndexes.

    In your next button code, do the following:
    • Get a random index.
    • Use objectAtIndex to extract a filename from your array of filenames.
    • Use UIImage imageNamed to load that image.
    • Install that image into your image view.
    • Create an NSNumber out of your random index using NSNumber numberWithInteger.
    • Add the NSNumber to your previousImageIndexes.
    In your back button method, get the last item in the previousImageIndexes array of NSNumbers. (There is an NSArray method lastObject to do that for you)

    Get the integer value of that entry from previousImageIndexes, and use it to index into your array of image names.

    Just like you did for your next button, load the image and install it in your image view.

    Finally, delete the NSNumber object from the previousImageIndexes array using removeObject or removeObjectAtIndex.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
Sign In or Register to comment.