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]
Related
I had made dropdown list of items. The list opens when i click the button and value appear on that button now i want that value to store in a string. I'm trying it with NSString *button=[Country text]; but it is displaying error.
Use this if you want get title from Button
NSString *button = [[Country titleLabel] text];
just a line of code:
NSString *reciever = Country.titleLabel.text
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;
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.
I'm practicing some iOS programming. I'm looking to move a UIImageView object to a given position on the screen. The storyboard contains
-Three textboxes to input X Y and Z components (xbox,ybox,zbox)
-Three labels to display said component values (XLabel,YLabel,ZLabel)
-UIImageView containing an image (ball)
-Go button (calls buttonPressed when pressed)
The following code successfully moves the image after inputing values into the textboxes n clicking "GO". However, when I uncomment the code to update the labels, the image does not move until I click "GO" twice in a row. The first time, the labels updates. The second time, the image updates. Why does it require two clicks when I have the label update code uncommented?
Thanks,
Rob
- (IBAction)buttonPressed {
[self graph: [xbox.text intValue] :[ybox.text intValue] :[zbox.text intValue]];
/*
XLabel.text = [NSString stringWithFormat:#"X:%#", xbox.text];
YLabel.text = [NSString stringWithFormat:#"Y:%#", ybox.text];
ZLabel.text = [NSString stringWithFormat:#"Z:%#", zbox.text];
*/
}
...
-(void)graph:(int)xpos : (int)ypos : (int)zpos{
ball.center = CGPointMake(xpos,ypos);
}
I'm going to provide this as an answer. Turn off auto-layout.
I have my UIPickerWheel setup to spin by a flick and populate a UILabel with whatever word value it lands on and that works just fine.
This issue that I'm having is when I added a button that spins my picker wheel and randomly chooses a word value. It spins the wheel just fine, and it populates my UILabel as well, but the UILabel and what is showing on the picker wheel doesn't match. I'm going to paste the code below.
I know that the way I have it setup right now is basically saying when user presses the button, spin the wheel and select a random value, and set the Text to label for the selectedItem. Right now the selectedItem = ? is where I'm having the issue. if I enter a string (e.g. #"hello") the label will say "hello" no matter what is lands on and if I type a number value of 0-5610 (the words on my array) then it will show that word.
Right now I have it just randomly choose a word. So I know that issue is on that line.
The line where im assuming my issue is coming from would be:
selectedItem =
/// HERE'S THE CODE///
- (IBAction)spin:(id)sender {
[pickerView selectRow:(arc4random() % [self pickerView:pickerView numberOfRowsInComponent:0]) inComponent:0 animated:YES];
selectedItem = [words objectAtIndex:(arc4random() % [words count])];
[label setText:selectedItem];
}
You are creating two different random numbers. You set the picker with one and choose a word from the word list with the other. Instead, create one random number. Use it for both operations:
- (IBAction)spin:(id)sender {
NSUInteger number = arc4random_uniform(words.count);
[pickerView selectRow:number inComponent:0 animated:YES];
selectedItem = words[number];
label.text = selectedItem;
}
This assumes the words in the picker match the words in the words array.