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

mandude

macrumors member
Original poster
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
 
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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.