Changing the text of a label on button press with dynamic integer - ios

I'm following a tutorial to create a simple game where you tap a button and the the game counts how many times you have pressed the button as you go along. The score is displayed on screen in the form of a label.
I can make it so that when you press the button the label text changes to say 'Pressed!'. But then i can not get the label to change when i try to add the changing score with a format specifier.
-(IBAction)buttonPressed{
count ++;
//scoreLabel.text =#"pressed";
scoreLable.text = [NSString stringWithFormat:#"Score\n%i", count];
}
Any help would be greatly appreciated. Thanks!

You may not have your label set up for multiple lines of text. Try getting rid of the "\n" in your format string. Other than that, you need to tell us what happens. Have you put a breakpoint to make sure your button IBAction is being called? Have you checked to make sure "scoreLable" (sic) is not nil? The devil is in the details.

-(IBAction)buttonPressed : (id) sender{
UIButton * btnPressed = (UIButton *)sender;
int buttonValue = [[btnPressed titleForState:UIControlStateNormal] intValue];
NSLog(#"Button value = %d", buttonValue);
//scoreLabel.text =#"pressed";
scoreLable.text = [NSString stringWithFormat:#"Score\n%i", count];
}

First You Can Set static int count = 0;

Related

Adding label text together

Hi im relatively new to iOS and I'm just wondering how i would add two labels together.
- (IBAction)switch1
{
if (switch1.on) {
value1.text = #"3";
} else {
value1.text = #"0";
}
}
- (IBAction)switch2
{
if (switch2.on) {
value2.text = #"3";
} else {
value2.text = #"0";
}
}
As you can see i have used two switches which would show two different values if they were turned on or off.
Could someone help me understand how i would add two values together.
I.e if switch one was on and switch two was off the value would be three i want this value to be shown in another label.
So far i have come up with this but for some reason it doesn't work, i have a feeling it is the format specifier but I'm not sure.
int sum = [[value1 text] intValue] + [[value2 text] intValue];
value3.text = [NSString stringWithFormat:#"%d", sum];
Dont you have this:
int sum = [[value1 text] intValue] + [[value2 text] intValue];
value3.text = [NSString stringWithFormat:#"%d", sum];
in ViewDidLoad or something? Because you have to call this at the end of both IBActions. If you don't, your final value will never change.
Make sure you properly created an outlet connection in Interface Builder as described here:
Creating an Outlet Connection
In short words. Ctrl+Click & Drag from the UISwitch to File’s Owner and click the new switch1 or switch2 action. Create outlets for the text fields and switches and link them.
Set breakpoints in switch1 and switch2 methods and ensure there are being called.
Use po command in the console to check if the text fields and switches are configured correctly.
For example:
po _textField1
should print text field's description. It will print nil when the text field is not there - not linked to a control in interface builder.

How do I use a UIButton to change a label multiple times?

I want to have one UIButton that will change the text of a label in a series. For example, I may have a label that says hello.
Then when i push a button, it will change to, What's up?.
But then a second tap of the same button will change the label to Nuttin' much!.
I know how to make the text of a label change once, but how do I change it many times with the same button? Preferably, anywhere around 20 to 30 separate texts.
Thank you in advance! :D
That's pretty open ended. Consider adding a property to your class which is an index into an array of strings. Each time you push the button increment the array (modulo size of array) and use the corresponding string to update the button. But there are a lot of other ways you could do this...
What happens when the app runs out of phrases? Start over? The typical approach would look like this.
#property (strong, nonatomic) NSArray *phrases;
#property (assign, nonatomic) NSInteger index;
- (IBAction)pressedButton:(id)sender {
// consider doing this initialization somewhere else, like in init
if (!self.phrases) {
self.index = 0;
self.phrases = #{ #"hello", #"nuttin' much" }; // and so on
}
self.label.text = self.phrases[self.index];
self.index = (self.index == self.phrases.count-1)? 0 : self.index+1;
}
In the viewDidLoad method, create an array with strings to hold the labels. Then create a variable to keep track of which object should be set as the current label. Set the initial text:
NSArray *labelNames = [[NSArray alloc] initWithObjects:#"hello",#"what's up?", #"nuttin much"];
int currentLabelIndex = 0;
[label setText:[labelNames objectAtIndex:currentLabelIndex]];
Then in the method that gets called when the button is tapped, update the text and the index.
- (IBAction) updateButton:(id)sender {
// this finds the remainder of the division between currentLabelIndex+1 and labelNames.count. If it is less than the count, its just the index. If its equal to the count we go back to the beginning of the array.
currentLabelIndex = (currentLabelIndex+1)%labelNames.count;
[label setText:[labelNames objectAtIndex:currentLabelIndex]];
}

iOS: Moving a uibutton by its tag outside of where it was generated

Is it possible? I have an array of buttons created in an void and I want to move one when a neighbouring one is called in the buttonTapped void. I have no trouble moving the button thats pressed because it is the sender, but I also need to move the one next to it and can't seem to get it to move. Each button in the array has a tag value so they're unique.
Thanks
Reasonable intuitive assumption: you generated the buttons so that they are ordered in the array...
- (void)moveButton:(UIButton *)sender // whatever
{
NSUInteger idx = [buttonArray indexOfObject:sender] + 1;
UIButton *nextButton = idx < buttonArray.count ? [buttonArray objectAtIndex:idx] : nil;
// do something with `nextButton`
}

How to use UITextFields?

I'm doing the following in my App right now:
NSInteger myReell1 = 6;
NSInteger myReell2 = 7;
NSInteger myImag1 = -5;
NSInteger myImag2 = 3
+ (WSData *)scientificData
{
float dataSet2D[2] = {0,myReell1 + myReell2};
float dataSet3D[2] = {0,myImag1 + myImag2};
return [WSData dataWithValues:[WSData arrayWithFloat:dataSet2D len:2]
valuesX:[WSData arrayWithFloat:dataSet3D len:2]];
}
This worked fine for me up to now.
Here is what I would like to change and what I would like to ask you for:
I would like to use 4 UITextFields to give the opportunity of personal input to the user, instead of the consistent Integers (myReell1, … ,myImag2) which I use right now.
How would you implement those UITextfields (maybe just 1 example?) into the code above to make the NSInteger part needless.
Please don't mind about the actual stuff inside of the scientific data. ;-)
Presuming this code is inside a view controller, you can:
Create your UITextField member variables in the class.
Subscribe the class to UITextFieldDelegate.
Wire your text fields up in Interface Builder (or instantiate them programatically and add them to the view).
Implement the didFinishEditing delegate method to handle the
actual input.
Say you make 4 textfield boxes, you can get the integer from going
myReell1 = [textFieldOne.text intValue];
and so on, there you have an integer you can use constantly, with no hardcoding
First put Textfield and then when user click some button just put bellow code in you button click metho...
NSInteger myReell1 = [textField1.text intValue];
NSInteger myReell2 = [textField2.text intValue];
NSInteger myImag1 = [textField3.text intValue];
NSInteger myImag2 = [textField4.text intValue];
:)

write with my buttons

I've made my own keyboard with some buttons made A-Z button and gave them Titles A button has title A, B button has title B and so on.
I added a label so when I click a button it would display the title of that button and it does.
When I click A it displays A, when I click B it displays B. The problem is that I want them to display next to each other but I can't get it work. like when I press ABC it would display ABC not first A then replace it with B and then replace it with C, because that is what it does now.
-(IBAction) clicked: (id)sender{
NSString *titleOfButton = [sender titleForState:UIControlStateNormal];
NSString *newLabelText = [[NSString alloc] initWithFormat:#"%#", titleOfButton];
labelsText.text = newLabelText;
}
Here is some code from a calculator where it does work but I can't find the right way to implant it with my code
//-(IBAction)buttonDigitPressed:(id)sender {
//currentNumber = currentNumber*10 + (float)[sender tag];
//calculatorScreen.text = [NSString stringWithFormat:#"%2f",currentNumber];
//}
is there any1 who can tell me how to get this working or knows an entire other way to get it working i've been looking and trying for almost a full week now thanks
This is the line that is causing you problems:
labelsText.text = newLabelText;
You are setting the label text to be ONLY newLabelText. What you want to do is concatenate the strings, which will combine the old text with the new text. This one change should do it:
labelsText.text = [NSString stringWithFormat:#"%#%#", labelsText.text, newLabelText]

Resources