Still Given It Hell

Discussion in 'iOS Programming' started by jessejohnson147, Jun 30, 2009.

  1. jessejohnson147 macrumors newbie

    Joined:
    Jun 5, 2009
    #1
    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
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Let's start with you're declaring imagePath as a UIImageView and then assign it an NSString. That's not gonna work.
     
  3. jessejohnson147 thread starter macrumors newbie

    Joined:
    Jun 5, 2009
    #3
    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};
    }
    
     
  4. harry65 macrumors member

    Joined:
    Aug 26, 2008
    #4
    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]...
     
  5. jessejohnson147 thread starter macrumors newbie

    Joined:
    Jun 5, 2009
    #5
    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?
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    Determine your random integer before you need it. Have a loop that gets another random integer if 5, 6 or 7 was chosen.
     
  7. jessejohnson147 thread starter macrumors newbie

    Joined:
    Jun 5, 2009
    #7
    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];
    }
    
    
    
    
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    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))
     
  9. jessejohnson147 thread starter macrumors newbie

    Joined:
    Jun 5, 2009
    #9
    Thank You You Have Been A Great Help. I Appreciate Everything.
     
  10. jessejohnson147 thread starter macrumors newbie

    Joined:
    Jun 5, 2009
    #10
    [imagePath setImage:ranImage];
    i keep getting a error right here
    passing argument 1 "setImage" from distinct objective c type
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    Is ranImage still an NSString? setImage: expects a UIImage-type parameter.
     
  12. jessejohnson147 thread starter macrumors newbie

    Joined:
    Jun 5, 2009
    #12
    yeah its still a NSString the only thing i changed was ObjImageview to imagePath
     
  13. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #13
    You still need to pass setImage: a UIImage and not an NSString. Try this:
    Code:
    [imagePath setImage:[UIImage imageNamed:ranImage]];
     
  14. jessejohnson147 thread starter macrumors newbie

    Joined:
    Jun 5, 2009
    #14
    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
     
  15. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #15
    UISwitch has no 'off' property, just 'on'.
     
  16. jessejohnson147 thread starter macrumors newbie

    Joined:
    Jun 5, 2009
    #16
    ok that makes sense can you help me with my loop
     
  17. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #17
    What do you mean it's freezing? Is it stuck in the loop? What does your changePicture: code look like now?
     
  18. jessejohnson147 thread starter macrumors newbie

    Joined:
    Jun 5, 2009
    #18
    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;}
     
  19. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #19
    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...
     
  20. jessejohnson147 thread starter macrumors newbie

    Joined:
    Jun 5, 2009
    #20
    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
     
  21. jessejohnson147 thread starter macrumors newbie

    Joined:
    Jun 5, 2009
    #21
    well i found out that part was just a stupid little error i didn't put all the images into the project
     
  22. boyplunder macrumors regular

    boyplunder

    Joined:
    Sep 8, 2008
    Location:
    UK
  23. jessejohnson147 thread starter macrumors newbie

    Joined:
    Jun 5, 2009
    #23
    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
     
  24. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #24
    Isn't working how? C'mon, we really need more details if you expect us to help you. :mad:
     
  25. jessejohnson147 thread starter macrumors newbie

    Joined:
    Jun 5, 2009
    #25
    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
     

Share This Page