Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jessejohnson147

macrumors newbie
Original poster
Jun 5, 2009
29
0
OK so the last couple of months all I've done is read books and tutorials in my free time... and I seem to be stuck with this problem... I came on here about a month ago and realized that I need to go back to the books but i have everything else for the app done I just can't get this stupid little thing checked off my list...

OK here it is im trying to make a random image viewer and when I hit the next player button it will rerandomize the images.

here is what I did:

I named all the images image1.png, image2.png and so on


In the .h file I have a outlet called imagePath

IBOutlet UIImageView *imagePath;

and a IBAction

- (IBAction)nextImage;

in the .m file

- (IBAction)nextImage {

(this is the peice of code someone gave me last month
NSString *imagePath = [NSString stringWithFormat:mad:"image%d.png", (arc4random() % 10) + 1];

}

any help is greatly appreciated
 
Code:
.h///////////////////////////////////////


IBOutlet UIImageView *imagePath;{
}

@property(nonatomic, retain) UIImageView *imagePath;
-(IBAction)changePicture :(id)sender;


.m/////////////////////////////////////////


@synthesize imagepath;

-(IBAction)changePicture {

NSString *ranImage = [NSString stringWithFormat:@"image%d.png", (arc4random() % 10) + 1];
imagePath = ranImage;  (I know there has to be a problem here)
[ranImage release};
}
 
Code:
-(IBAction)changePicture {

NSString *ranImage = [NSString stringWithFormat:@"image%d.png", (arc4random() % 10) + 1];
imagePath = ranImage;  (I know there has to be a problem here)
[ranImage release};
}

Instead of the above, would something like this work:

Code:
-(IBAction)changePicture {

NSString *ranImage = [NSString stringWithFormat:@"image%d.png", (arc4random() % 10) + 1];
imagePath = [[NSImageView alloc] initWithImage:[UIImage imageNamed:ranImage]];
}

Also, I don't think you want to release ranImage since I believe stringWithFormat returns an auto-released object.

Not sure if [UIImage imageNamed] is what you want - maybe try [UIImage imageWithContentsOfFile]...
 
Code:
.h///////////////////////////////////////


IBOutlet UIImageView *imagePath;{
}

@property(nonatomic, retain) UIImageView *imagePath;
-(IBAction)changePicture :(id)sender;


.m/////////////////////////////////////////


@synthesize imagepath;

-(IBAction)changePicture {

NSString *ranImage = [NSString stringWithFormat:@"image%d.png", (arc4random() % 10) + 1];
[ObjImageview setImage:ranImage];

[ranImage release];
}

This should work but there is more

Ok now say I have a switch that is turning off mountain images. Mountain images are numbered 5,6, and 7. So 1,2,3,4,8,9,and10 still have to randomize. What is the best way to go about this?
 
so would tis make it loop

Code:
.h///////////////////////////////////////


IBOutlet UIImageView *imagePath;{

}
IBOutlet UISwitch *mountainSwitch;{

}

@property(nonatomic, retain) UISwitch *mountainSwitch;
@property(nonatomic, retain) UIImageView *imagePath;
-(IBAction)changePicture :(id)sender;


.m/////////////////////////////////////////


@synthesize imagepath;

-(IBAction)changePicture {

int randNumber = (arc4random() % 10) + 1;

while( mountainSwitch.off && randNumber == 5 || mountainSwitch.off && randNumber == 6 || mountainSwitch && randNumber == 7) 

    randNumber = (arc4random() % 10) + 1;

NSString *ranImage = [NSString stringWithFormat:@"image%d.png", randNumber];

[ObjImageview setImage:ranImage];

[ranImage release];
}
 
so would tis make it loop

Code:
.h///////////////////////////////////////


IBOutlet UIImageView *imagePath;{

}
IBOutlet UISwitch *mountainSwitch;{

}

@property(nonatomic, retain) UISwitch *mountainSwitch;
@property(nonatomic, retain) UIImageView *imagePath;
-(IBAction)changePicture :(id)sender;


.m/////////////////////////////////////////


@synthesize imagepath;

-(IBAction)changePicture {

int randNumber = (arc4random() % 10) + 1;

while( mountainSwitch.off && randNumber == 5 || mountainSwitch.off && randNumber == 6 || mountainSwitch && randNumber == 7) 

    randNumber = (arc4random() % 10) + 1;

NSString *ranImage = [NSString stringWithFormat:@"image%d.png", randNumber];

[ObjImageview setImage:ranImage];

[ranImage release];
}
That should work. I'd re-code the while-conditional to avoid the unnecessarily repeated mountainSwitch.off, like so:
Code:
while( mountainSwitch.off && (randNumber == 5 || randNumber == 6 || randNumber == 7))
 
[imagePath setImage:ranImage];
i keep getting a error right here
passing argument 1 "setImage" from distinct objective c type
 
that did the trick but the switch and the loop are not correct I get a error on the mountainSwitch.off saying it is not structure or union and I switched it to on just to check the loop and it is not looping it just freezes when it get on 5,6, or 7
 
sorry it took so long to get back... i just had to move somethings from this
Code:
int randNumber = (arc4random() % 10) + 1;

while( mountainSwitch.off && randNumber == 5 || mountainSwitch.off && randNumber == 6 || mountainSwitch && randNumber == 7) 

    randNumber = (arc4random() % 10) + 1;

NSString *ranImage = [NSString stringWithFormat:@"image%d.png", randNumber];


to this
Code:
int randNumber = (arc4random() % 10) + 1;



NSString *ranImage = [NSString stringWithFormat:@"image%d.png", randNumber];

while( mountainSwitch.off && randNumber == 5 || mountainSwitch.off && randNumber == 6 || mountainSwitch && randNumber == 7) 

    randNumber = (arc4random() % 10) + 1;
it works just fine now and instead of using switches I decided to use a segmented a control to toggle them on and off like so
Code:
-(IBAction)changeSegOne{

while(segControlOne.selectedSegmentIndex == 1 && (randNumber == 5 || randNumber == 6 || randNumber == 7))

    randNumber = (arc4random() % 10) + 1;}
 
So your new code is?:
Code:
int randNumber = (arc4random() % 10) + 1;

NSString *ranImage = [NSString stringWithFormat:@"image%d.png", randNumber];

while( mountainSwitch.off && randNumber == 5 || mountainSwitch.off && randNumber == 6 || mountainSwitch && randNumber == 7) 

    randNumber = (arc4random() % 10) + 1;

Hmm, looks like you are now setting the ranImage string before you have even checked if you need to avoid 5, 6 or 7. So, that while loop is kinda useless. Plus, you still a referencing an 'off' property which UISwitch doesn't have. I'm not sure how it could be working just fine now...
 
yeah sorry pasted the wrong thing here it is
Code:
int randNumber = (arc4random() % 17) + 1;
	
	while(shots.selectedSegmentIndex == 1 && randNumber == 7)
		randNumber = (arc4random() % 17) + 1;
	
	
	
NSString *ranImage = [NSString stringWithFormat:@"image%d.png", randNumber];
	[imagePath setImage:[UIImage imageNamed:ranImage]];

the only problem i have is once every so often it wont pick a image and puts a blank screen
 
well i found out that part was just a stupid little error i didn't put all the images into the project
 
UGH .... the loop was working last night then i got on today and it isn't working anymore .... it is exactly the same as i posted it last night
 
sorry I was just getting flustered .... it randomises perfectly just the loop for the segemented controls are not working it still pulls up the images ... there are no errors what so ever its just not looping
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.