How to reveal text line by line - Xcode - objective-c

I have a simple app, I have a button that when pressed will display separate lines of text on the screen. I set an int = 0 and use an if statement that will add 1 at each line. The issue I have is that all lines are displayed after 1 button press, I want 1 button press one line displayed, next button press next line displayed and so on:
int nmbr = 0;
int myInt1 = [textfield.text intValue];
if (nmbr == 0) {
tString = [[NSString alloc] initWithFormat:#"111111111111%i x 1 = %i",myInt1,myInt1];
nmbr++;
}
if (nmbr == 1){
tString1 = [[NSString alloc] initWithFormat:#"222222222222%i x 2 = %i",myInt1,myInt1*2];
nmbr++;
}
if (nmbr == 2){
tString2 = [[NSString alloc] initWithFormat:#"%i x 3 = %i",myInt1,myInt1*3];
nmbr++;
}
I am new to this so any help is greatly appreciated.
Update:
Thanks folks for your help so far, I am not sure that I have explained this issue well……possibly gone about programming it all wrong. I have only one button on screen and each time it is pressed it would change a blank label to a text string. I was incrementing in each if statement so that on the next button press as nmbr = 2 then text label 2 would be modified, press again nmbr = 3 text label 3 would be modified and so on. There appears to be no separation in the code so on the button press all text labels are modified. I am not sure if it is possible to code multiple actions on a single button or if there is a better way to do this.
I have tried else if statements but still no joy.

Move your nmbr++; to the end of the method instead of putting it in each if statement. If you don't want it to increment every time then use a bool to keep track of whether or not you want to increment it. Also, nmbr needs to be a member variable (i.e. class level) instead of method level. If it is method level then its state isn't saved between calls.

Your result is normal because you if test are coming one after each other. The program will go on the first if, incremented nmbr, go out of the if and hit the next if.
It can work by moving the nmbr incrementation out of the "if" (as mentioned by mydogisbox) but you will still have to declare the variable as static.
Something like this:
static int nmbr = 0;
int myInt1 = [textfield.text intValue];
if (nmbr == 0) {
tString = [[NSString alloc] initWithFormat:#"111111111111%i x 1 = %i",myInt1,myInt1];
}
if (nmbr == 1){
tString1 = [[NSString alloc] initWithFormat:#"222222222222%i x 2 = %i",myInt1,myInt1*2];
}
if (nmbr == 2){
tString2 = [[NSString alloc] initWithFormat:#"%i x 3 = %i",myInt1,myInt1*3];
nmbr = 0; // if you have no other lines and you want to start from begining when the user press the button more than 3 times.
}
nmbr++;

Create required number of buttons in Interface Builder, declare them into .h file, create methods for each button -(void)firstButtonClick:(UIButton)sender, -(void)secondButtonClick:(UIButton)sender and put your code into these methods.

The problem is that you're incrementing within the if and then letting it fall to the next. Look at the flow: nmbr is one, so execute that block, which increments nmber to 1, making the next block true also. Depending on your style guides or personal preferences, you could:
Change the later ifs to else if.
Move all the nmbr++s to after the last if.
Return after incrementing inside the if.

Related

Objective C: Why is Button moving?

I've got a rather strange bug in my app. Whenever I press the second Button that makes the Method go into the "if", one of my Buttons moves a bit to the left.
It looks like this: http://img823.imageshack.us/img823/3686/30000000.jpg
My App:
I've got 12 buttons in a row with Letters on them. Every button has a label above it.
The labels are called Label0, Label1, Label 2,...
There is a word in the String myString. If the button pressed has the first letter of the word on it -> write it in the first label -> look at second letter of the word...
/// Global ///
start = 4;
letterCount = start;
currentChar = [NSString stringWithFormat:#"%c",[myString characterAtIndex:0]];
- (IBAction)pushButton:(id)sender {
UILabel *label = [self valueForKey:[NSString stringWithFormat:#"Label%i" , letterCount]];
if ([[sender currentTitle] isEqualToString:currentChar]) {
label.text = currentChar;
label.hidden = NO;
if (letterCount-start < [myString length]-1) {
letterCount++;
}
currentChar = [NSString stringWithFormat:#"%c",[myString characterAtIndex:letterCount-start]];
}
Nothing in that code seams to fool around with the position of the buttons. Nevertheless its moving... every time...
I'm using xCode 4.6.1
Thanks,
Michael
Try removing that button and adding a new one (giving the new button the same name as the old one) and re-attach all of your IB stuff and see if that fixes it.

Setting UILabel Text Multiple Times Within A Loop

I'm fooling around in XCode, trying to learn a little about the iOS SDK and Objective-C.
I have this for loop below, and it should print out several values to the screen (depending on the amount of months chosen), but instead, it's only printing out the final value.
Can anybody point out why?
Thanks a bunch, in advance!
for (int i = 1; i <= myMonthsDouble; i++)
{
myPaymentAmount = (myBalanceDouble/10) + myInterestDouble;
myBalanceDouble -= myPaymentAmount;
//convert myPaymentAmount double into string named myPaymentAmountString
NSString *myPaymentAmountString = [NSString stringWithFormat:#"%f", myPaymentAmount];
NSString *paymentInformation = [[NSString alloc] initWithFormat:#"%# months, %# per month.", monthsString, myPaymentAmountString];
myInterestDouble = (myBalanceDouble * (myInterestDouble/100))/12;
self.label.text = paymentInformation;
}
It is only printing the last value to the screen because you only have one label. Each time you get to the end of the loop, you are setting that label's text which is overriding the last value. If you want to print all of them to the screen, you will need to have either multiple labels or you will have to append the strings together and put them in either a label or a UITextView that is formatted so that they can all be seen (most likely a text view but it can be done with a label.)
One example of doing this would be:
label.text = [label.text stringByAppendingString:newString];
numLines++; //this starts at 0;
and then at the end:
label.numberOfLines = numLines;

IBAction to display next object of array

I'm trying to do something I thought was going to be very simple but I can't figure it out. I'm filling an array with objects like this in TableViewController:
if (indexPath.row == 0) {
detailController.textArray = [[NSMutableArray alloc] initWithObjects:#"Text1", #"Text2", #"Text3", #"Text4", #"Text5", nil];
}
Then in the DetailController I added this action to make the label display the next object of the textArray:
- (IBAction)nexTextButtonPressed:(id)sender {
int i = 0;
if (i<[textArray count])
i++;
textLabel.text = [textArray objectAtIndex:i];
}
I connected the button in ib to the action with option Touch Down (tried some of the others too). Something isn't working, the button jumps to the second object of the array but then it doesn't work any more. What could be causing this and how to fix it?
int i = 0; is initialized inside the method, it will always be reinitialized to zero on every call which will not let you move forward from second object
Well every time you hit the button i is getting reset to 0 so when it hits the if statement yes it goes into the statement as the condition is correct, but then you are incrementing i by 1 so i becomes 1 then you retrieve the array index 1 every time. You aren't actually doing anything else. No loop to print through the array or anything.

Blank textField (Calculation) crashes app.

I have an app that does a simple calculation from 2 text fields, the result is then displayed as label text. This is fine as long at each text field is populated, if no data is entered and the calculate button is pressed then the app closed. I assumed that blank would return a value of 0 and result would be displayed as 0. My code is as follows:
-(IBAction) tonerCalc: (id) sender{
NSString *tString;
int myInt = [textField1.text intValue];
int myInt1 = [textField2.text intValue];
int total = myInt/(myInt1/5);
tString = [[NSString alloc] initWithFormat:#"%i",total];
labelText.text = tString;
[tString release];
}
How can I stop the crash, I think it has something to do with the calculation. Any help is greatly appreciated.
intValue will return 0 because there is no number in the string. But then you're doing a division by zero.
You can put a check like
`if(![textField1.text isEqualToString:#""]|| ![textField2.text isEqualToString:#""])`
{
//perform calculation
}
else
{
//show alert that text field are empty
}
and if the condition is true then you can make your calculation do otherwise show alert.It will stop the crash.

Why does the ++ operator increase an integer by 4 and not by 1?

I've got some simple code in Objective-C wich consist of a button and a label. When you click the button once, the labels shows: "u hit me", if you click it twice, the message will be: "i did it again". But, if u hit the button 5x or more, the message should be: "STOP THAT";
I used simple ifs and a counter that is increased using the operator ++. The problem is: My counter increases in steps of 4, and not in steps of one.
Here's the code
#implementation hitMe
NSString *myString = #"";
int *counter = 0;
- (IBAction)htM:(id)sender {
if ([myString isEqualToString:#""]){
//first hit
myString = #"u hit me";
} else {
// second and next hits...
myString = #"u did it again!";
counter++;
}
// if I use "counter > 5" it doesn't work,
// I have to use 21 if I want the button hit 5 times before
// I get the "STOP THAT" message
if (counter > 21) {
myString = #"STOP THAT ";
}
[labelOne setStringValue:myString];
// I used this only to check the count value
[labelTwo setIntValue:counter];
}
#end
The variable you're incrementing, counter, is a pointer to an int, not an int. So the compiler turns:
counter++;
into
counter += 4; // sizeof(int) == 4
which is what is needed to get to the next word in memory, or the next location you might point to. This might seem weird, but if you had an array of ints and a pointer that you were using to examine them, incrementing the pointer would take you to the next int (which is four bytes on most architectures these days).
Change int *counter to int counter and you should be fine.
Edit: The C language spec defines pointer arithmetic (what happens when you add or subtract with pointers) this way because the most common use of pointer math is to navigate around an array that you're pointing to. So one unit of increment for a pointer is one unit of the array, or sizeof(arrayType). A char* would have given you the behavior you expected, because sizeof(char) is 1.