Objective C: Why is Button moving? - objective-c

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.

Related

comparing text of a button to a label in objective-c

I have the following code:
int index = [self.colorlist count];
int random1 = (arc4random() % index);
NSString *color1 = [self.colorlist objectAtIndex:random1];
[Button1 setTitle:color1 forState:UIControlStateNormal];
I want to compare the Button 1 text to a different label with .isEqualTo but using
if button1.text isEqualToString: label
does not work. What is the proper syntax for calling button 1's text, rather than button1.text. Thanks!
There are two ways:
button.currentTitle
And
button.titleLabel.text
The first is a read-only reference to the current text on the button.
The latter is a readwrite reference to the text property of the label on the button. The label reference however (button.titleLabel) is itself readonly.
you can easily do it by following code
if([button.currentTitle isEqualToString(textField.text)])
{
//Succes part
}
else
{
//Failure Part
}
Hope this answer may help you

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;

Change LHSprite position from code (levelhelper-XCODE)

I'm trying to move all sprites with the same tag some inches to the right.
I have tried 4 different type of expressions in order to do so, but nothing worked. Here is what i ve done so far...
-(void) moveSprites {
NSArray* spritesWithTag = [lh spritesWithTag: BOXES1]
for (LHSprite* spr in spritesWithTag)
(NSLog (#"name is %#", [spr uniqueName]);
CGPoint newposition = ccpSub (ccp(-50,0), [spr position]);
//generating the new position for the elements
[spr transformPosition: newposition];
//first attemp, should work but for some reason it doesn't work
spr.position = newposition;
//2nd attemp
b2Vec2 newpositionVector (newposition.x, newposition.y);
[spr body]->SetTransform(newpositionVector, 0);
//third try
[spr setPosition:newposition];
//last form
}
When i run the app the method call works fine and all sprites with tag BOXES1 appear in the output tab, but its position hasn't changed at all. Any idea over why is it happening. What did I wrong? Is there any other way to move an sprite or are them prevented from moving in some other form i dont know? Those are static sprites, dont know if this affects... thanks!

Moving the cursor in an UITextView

I try to move the cursor when a UITextView is selected(touched) to simulate kind of a UITextField placeholder thing.
I'd like the cursor to be at the beginning of the first line. My problem is, that [someTextField setSelectedRange]is not working reliably. When I call it in textView:shouldChangeTextInRange:replacementText: it works as it should. But this method is only called when the user starts typing. I'm using textViewDidBeginEditing: to move the cursor when the UITextView becomes the first responder:
- (void)textViewDidBeginEditing:(UITextView *)textView
{
if (textView == self.descriptionText) {
CustomTextView* customTV = (CustomTextView *)textView;
if ([customTV.text isEqualToString:customTV.placeholder]) {
// text in text view is still the placeholder -> move cursor to the beginning
customTV.text = customTV.placeholder;
customTV.textColor = [UIColor lightGrayColor];
customTV.selectedRange = NSMakeRange(0, 0);
}
}
}
Any ideas why customTV.selectedRange = NSMakeRange(0, 0); isn't working correctly in textViewDidBeginEditing: ?
Thanks for your help!
Actually there's a very simple way of accomplishing this.
// Count the characters on screen
NSMutableString *numOfChar = [self.myTextField.text mutableCopy];
// Skip that many characters to the left
self.myTextField.selectedRange = NSMakeRange(self.myTextField.selectedRange.location-[numOfChar length], 0);
Hope this helps.

How to reveal text line by line - Xcode

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.