Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 01-07-2012, 10:48 PM   #1 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 8
chris Good is on a distinguished road
Default 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)nextbuttonid)sender;
{
int ptr = rand() % 6;
NSArray *images = [[NSArray alloc] initWithObjects:@"Americans.png",@"Approach.png",@ "Arianny.png",@"Atoms.png",@"Australia.png",@"Aver age.png",nil];

}

//I want to make a button that will display the previous image of the images in array above
chris Good is offline   Reply With Quote
Old 01-07-2012, 11:26 PM   #2 (permalink)
CiE
Registered Member
 
Join Date: Dec 2010
Posts: 36
CiE is on a distinguished road
Default

Quote:
Originally Posted by chris Good View Post
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)nextbuttonid)sender;
{
int ptr = rand() % 6;
NSArray *images = [[NSArray alloc] initWithObjects:@"Americans.png",@"Approach.png",@ "Arianny.png",@"Atoms.png",@"Australia.png",@"Aver age.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.
CiE is offline   Reply With Quote
Old 01-07-2012, 11:52 PM   #3 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 8
chris Good is on a distinguished road
Default

what code can I use to do this for a new button
chris Good is offline   Reply With Quote
Old 01-08-2012, 05:54 PM   #4 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

Also, a few points in that code make no sense.

Code:
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.

Code:
ptr = 5;
ptr--;
Why?? Why set the value to 5 then decrement it by 1. Why not just: ptr = 4;
baja_yu is offline   Reply With Quote
Old 01-08-2012, 05:55 PM   #5 (permalink)
Super Moderator
 
Join Date: Jan 2011
Posts: 434
Rhade is on a distinguished road
Default

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. Report the post or send a PM.
<----- Use the red flag button to report posts.
Rhade is offline   Reply With Quote
Old 01-08-2012, 08:29 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by chris Good View Post
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)nextbuttonid)sender;
{
int ptr = rand() % 6;
NSArray *images = [[NSArray alloc] initWithObjects:@"Americans.png",@"Approach.png",@ "Arianny.png",@"Atoms.png",@"Australia.png",@"Aver age.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

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Reply

Bookmarks

Tags
button, ios, previous button, uiimage

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 399
10 members and 389 guests
Atatator, buggen, guusleijsten, j.b.rajesh@gmail.com, morterbaher, QuantumDoja, sacha1996, Sami Gh, tim0504, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,674
Threads: 94,122
Posts: 402,907
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Atatator
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 05:35 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0