Reputation: 3494
This is a logic issue. For example imagine i have 4 Labels. And i need to set a number to these labelas its title.
The labels are follows; There will be a button and when i click on it, the Titles of the labels should change (This is similar to First in First Out
)
When i click the button for the first time it should just display the 1 for Label1. Then when i click the button for the 2nd time it should display values for label1 and label2 (The values will be 1 and 2 respectably). The problem arises on the 5th time we try to click. At this time the value of label1 (which was 1)will be erased and it should now take the value 2 (each number puss forward).
note: Only 4 numbers are allowed at an instance.
L1 --- L2 --- L3 --- L4
01 --- 02 --- 03 --- 04
02 --- 03 --- 04 --- 05
03 --- 04 --- 05 --- 06
And it goes on.
How could i do this programatically, and i have not coded any to show your'l code. I am stuck with this.
Upvotes: 1
Views: 64
Reputation: 832
Some pseudocode for what I think you're trying to accomplish (fill the labels one at a time until all are filled, then keep updating the labels to show only the latest set of numbers):
int numLabels = 4;
onClick () {
static int lastDisplayed = 0;
lastDisplayed++;
for (i from 1 to min(lastDisplayed, numLabels)) {
label[i-1] = (i + lastDisplayed - min(lastDisplayed, numLabels));
}
}
Upvotes: 0
Reputation: 31016
I'm assuming you only want to change one label per click. It's not exactly responding to clicks or setting labels, but here's something to examine that should give you the answer regarding the arithmetic:
NSInteger maxLabel = 4;
for (NSInteger clickCount = 0; clickCount < 12; clickCount++) {
NSInteger baseValue = clickCount / maxLabel;
NSInteger cycleValue = (clickCount % maxLabel) + 1;
NSLog(@"Set label %i to %i", cycleValue, baseValue + cycleValue);
}
On the other hand, if you're trying to change them all at once, then the logic is similar except save the baseValue and update all the labels when it changes.
Upvotes: 1
Reputation: 50220
Your description is not particularly clear. But it sounds like you're trying to describe a situation where more that one label can be "active" at a time, and pressing the button cycles through the states in some way. If so, your code should keep track of which labels are active at any time. To handle a key press, examine the current state and Do The Right Thing.
Or maybe you want to save the last four numbers, and keep incrementing? Then keep a list of the four values you have, and keep track of the next number to insert and of where it will go. To know where it will go, it's handy to use the modulo operation: n % 4 will go to 3, then wrap to 0.
Now go write some code.
Upvotes: 1