help on crashing code void value not ignored?

Discussion in 'iOS Programming' started by mandude, Jun 13, 2010.

  1. mandude macrumors member

    Joined:
    Nov 19, 2009
    #1
    so i am trying to have a list of UIButtons in an Array be chosen randomly one at a time and everytime it chooses a button, it makes the buttons set hidden to NO, then moves the button from the point (240, 30) to the point (30, 240). I then want the button chosen and moved to automatically delete itself from the array with all the buttons. this way the second time this event is called the buttons already moved wouldn't be able to be chose. here's my code for now... i know im not that good

    h. file

    Code:
    
    UIButton *theButton;
    
    
    @property (nonatomic, retain) IBOutlet UIButton *theButton;
    
    
    
    i didn't actually connect this UIButton to a round rect button in interface builder, im just attempting to createt a random uibutton that will be randomly selected and able to be called out by the universal name of "theButton"


    m. file

    Code:
    -(void)dealFirstCard{
     
    	theButton = [[entireCardDeckArray objectAtIndex: (random() % [entireCardDeckArray count])]setHidden:NO];
    
    	CGPoint faceCenter = theButton.center;
    	faceCenter.x = 40;
    	faceCenter.y = 195;	
    	theButton.center = faceCenter;
    
    	[UIView beginAnimations:@"Card1DealAnimation" context:nil];
    	[UIView setAnimationDuration:1];
    	CGPoint center1 = cardDeck.center;
    	center1.x = 32;
    	center1.y = 420;
    	
    
    
    	cardDeck.center = center1;
    	
    
    	[UIView commitAnimations];
    	
    	[entireCardDeckArray removeObject:theButton];
    
    
    }
    
    all help is appreciated
     
  2. PhoneyDeveloper macrumors 68040

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    Don't write code like this line

    Code:
    theButton = [[entireCardDeckArray objectAtIndex: (random() % [entireCardDeckArray count])]setHidden:NO];
    You are doing too many things in one line. It's impossible to tell if something in the middle isn't correct. Do these things on separate lines: get the random number, get the button object from the array, set the button to be not hidden. Then you can continue to set the center or whatever else needs to be done.

    If you write the code in a clear way like that it should work, or at least you should be able to step through it line by line in the debugger and see what's wrong in a clear way.
     
  3. admanimal macrumors 68040

    Joined:
    Apr 22, 2005
    #3
    One problem that I see is that theButton that is selected at random is not retained and will probably be dealloc'ed once it is removed from the entireCardDeckArray.

    Although you have defined a theButton property as retained, you're not actually using the synthesized accessor (I assume you synthesized it) when you assign the random button into theButton like this

    Code:
    theButton = ..whatever...
    
    so theButton is not retained by your view controller and is probably not retained by anyone once you remove it from entireCardDeckArray. This is bad.

    What you want to do is this:

    Code:
    self.theButton = ...whatever...
    
    which will actually use the synthesized accessor that does properly retain theButton, making it safe to use once the method exits.
     
  4. mandude thread starter macrumors member

    Joined:
    Nov 19, 2009
    #4
    Thanks both of you guys! I'll report back with news soon
     

Share This Page