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 > Mac OS X Development Forums > Objective-C, Python, Ruby Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-05-2011, 09:15 AM   #1 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 551
aldcorn@live.com is on a distinguished road
Default Determine if objecst in an array are below # x

Hi guys,
I am trying to work out a way to check if the index value of my objects in an array are below a certain number. This number changes.

Say I have 100 UIButtons in an array and I wanted to check if the index of each object is below or above my number. THEN if the object index is above the number I want to setHidden : YES; for all of those objects.

This is a first step in trying to understand the value of using arrays. Can anybody point me in the right direction?

Thanks,
Rob
aldcorn@live.com is offline   Reply With Quote
Old 02-05-2011, 10:32 AM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Here's one way to do exactly what you asked:

Code:
for (int i=0; i< [buttonArray count]; i++){
	UIButton *button = [buttonArray objectAtIndex:i];
	if(i> testIndex)
		button.hidden=YES;
}
That could be more efficient though - if you're not doing anything to the first N objects then you don't have to start at zero!

Code:
for (int i=testIndex+1; i< [buttonArray count]; i++){
	UIButton *button = [buttonArray objectAtIndex:i];
	button.hidden=YES;
}
Now it's only looping through the numbers that are higher than your index. However, if you call this with testIndex=5 and then again with testIndex=10 then you probably need to set hidden to NO on buttons 6-10, right? So you probably really need this:

Code:
for (int i=0; i< [buttonArray count]; i++){
	UIButton *button = [buttonArray objectAtIndex:i];
	BOOL shouldHide = (i>testIndex);
	button.hidden=shouldHide;
}
Now it decides, for each button, whether it needs to be hidden (YES or NO) and sets the property accordingly.
__________________

Free Games!

Last edited by smasher; 02-05-2011 at 06:24 PM.
smasher is offline   Reply With Quote
Old 02-05-2011, 02:08 PM   #3 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 551
aldcorn@live.com is on a distinguished road
Default thanks

Thanks for the quick reply. I will give this a try when I get home from work tonight.
I appreciate your help. I hope to gain a better understanding of how to use arrays and this will help my understanding greatly. (and I can use it in my current project )

Take care,
Rob
aldcorn@live.com is offline   Reply With Quote
Old 02-05-2011, 11:27 PM   #4 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 551
aldcorn@live.com is on a distinguished road
Default Hi, I really tried but I did not succeed.

Hi thanks for your help on this.
I spent an hour or so actually trying to follow this code and I really did try a number of variations of this code. I have also spent some time searching the net. Sorry for not picking this up right away...

This is my current attempt:
currentNumber is the SCORE I am using to set the display. currentNumber is working for me in other functions. currentNumber is the number I want to check my array index against to say any index above currentNumber should be set to hidden. AND YES like you suggested I want to check this again and again so I need to have the function determine every time whether the button should be hidden or not.
Code:
	for (int i=0; i< [arrayPlayer1btns count]; i++){
		UIButton *button = [arrayPlayer1btns objectAtIndex:i];
		BOOL shouldHide = (i>currentNumber);
		button.hidden=shouldHide;
	}
I create my array from instances of my buttons (is this my problem?)
Code:
arrayPlayer1btns = [NSArray arrayWithObjects:btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,
						btn10,btn11,btn12,..., nil];
In your example I am confused about testIndex. Does this sound like my currentNumber to you?
I am also more familiar with the [button setHidden: YES]; notation. IS this the same thing as your code is doing?

Thanks for your help.


ALSO, is there a way I can add a log somewhere to help me trouble shoot this?
aldcorn@live.com is offline   Reply With Quote
Old 02-06-2011, 12:58 AM   #5 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

The code you posted looks fine. What happens when you use it? You didn't say if you get a crash, wrong result, what.

Quote:
Originally Posted by aldcorn@live.com View Post
I create my array from instances of my buttons (is this my problem?)
Code:
arrayPlayer1btns = [NSArray arrayWithObjects:btn0,btn1,btn2,btn3,btn4,btn5,btn6,btn7,btn8,btn9,
						btn10,btn11,btn12,..., nil];
That looks fine; where do you call that code, though? That will produce an autoreleased array which will be destroyed at the end of the current run loop unless you retain it. You should probably log the count of that array and see if it contains the number of items you expect.

Quote:
In your example I am confused about testIndex. Does this sound like my currentNumber to you?
Yes.
Quote:
I am also more familiar with the [button setHidden: YES]; notation. IS this the same thing as your code is doing?
Yes, button.hidden=YES is just shorhand for [button setHidden: YES].

Quote:
ALSO, is there a way I can add a log somewhere to help me trouble shoot this?
Yes, you could NSLog the array and each button as you set them.

Code:
NSLog(@"Array is: %@",arrayPlayer1btns);

	for (int i=0; i< [arrayPlayer1btns count]; i++){
		UIButton *button = [arrayPlayer1btns objectAtIndex:i];
		BOOL shouldHide = (i>currentNumber);

		NSLog(@"button:%x shouldHide:%d",button, shouldHide);

		button.hidden=shouldHide;
	}
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-06-2011, 01:17 AM   #6 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 551
aldcorn@live.com is on a distinguished road
Default Hey

Thanks for sticking with me here...
I used some nslogs to see if my action to run the check was actually firing. It was but only if I put the log outside the for statement.

I also tried moving the array alloc to different spots with varying results. I was checking to see if there were any objects in this array. Sometimes yes, sometimes no.

How do I create an array that is not autoreleased? Would this be a better option for me? I essentially have a score board where players are adding their score per round and adding it to the total. In this case the total is represented by currentNumber. Currentnumber basically exist for the entire game and is actually stored and brought back in when relaunching.

Where do you suggest I put the code? I have a button that basically submits the current turn score to the total, that is where I was trying to put the for statement. I had the array alloc in view did load and also in an alert view button action that I present every time the app launches.
It is pretty much a single viewController app.

Just fyi I want the buttons to show or hide every time the score is updated. Currently this is done when submitting the current turn score by selecting a button ( not any button from the array, separate)

Hopefully that is descriptive enough.

EDIT:
Thanks form the nslog code I will try that to see if I can figure anything out.
Rob

Rob

Last edited by aldcorn@live.com; 02-06-2011 at 01:21 AM.
aldcorn@live.com is offline   Reply With Quote
Old 02-06-2011, 10:03 AM   #7 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 551
aldcorn@live.com is on a distinguished road
Default i will work some more at this..

I will try your advice and use some nslogs to see what makes sense for my project and hopefully I can sort it out.

I will update.

Thanks,
Rob
aldcorn@live.com is offline   Reply With Quote
Old 02-07-2011, 02:30 AM   #8 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 551
aldcorn@live.com is on a distinguished road
Default Got it working!

Smasher thanks for all you help and explanation.
For anyone following this thread with a similar problem I will give a more complete description of what I did to get things working for me tomorrow, once I get some sleep!

One major mistake was adding an item that did not exist to my array!
aldcorn@live.com is offline   Reply With Quote
Old 02-07-2011, 04:08 PM   #9 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 551
aldcorn@live.com is on a distinguished road
Default

Quote:
Originally Posted by aldcorn@live.com View Post
Smasher thanks for all you help and explanation.
For anyone following this thread with a similar problem I will give a more complete description of what I did to get things working for me tomorrow, once I get some sleep!

One major mistake was adding an item that did not exist to my array!
Code:
-(IBAction)hideMyButtons:(id)sender {
	NSLog(@"hideMyButtons Array is: %@",arrayPlayer1btns);
	
	for (int i=0; i< [arrayPlayer1btns count]; i++){
		UIButton *button = [arrayPlayer1btns objectAtIndex:i];
		BOOL shouldHide = (i>currentNumber+1);
		
		NSLog(@"button:%x shouldHide:%d",button, shouldHide);
		
		button.hidden=shouldHide;
		}	
}
created the array here: Note that I retained it.
Code:
- (void)viewDidLoad {	
	[addToABtn setHidden:YES];
	arrayPlayer1btns = [[[NSArray alloc] initWithObjects:btn1,btn2,btn3,btn4,btn5, nil]retain];
	NSLog(@"viewDidLoad Array is: %@",arrayPlayer1btns);
...}
And I used this to trouble shoot what and where my array was. I put this log all over the place to help me follow where and when my array was created and holding objects:
Code:
NSLog(@"From addTo... Array is: %@",arrayPlayer1btns);
Smasher thank you very much.

Cheers,
Rob
aldcorn@live.com is offline   Reply With Quote
Reply

Bookmarks

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: 488
16 members and 472 guests
7twenty7, AlanFloyd, AppsBlogger, David-T, iAppDeveloper, imac74, Jaxen66, lovoyl, Music Man, mutantskin, Paul Slocum, Sami Gh, SLIC, solardrift, unicornleo, usernametaken
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,683
Threads: 94,131
Posts: 402,932
Top Poster: BrianSlick (7,990)
Welcome to our newest member, unicornleo
Powered by vBadvanced CMPS v3.1.0

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