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?
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:
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 )
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.
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?
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.
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.
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!
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!
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);