Get value from Button - ios

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

Related

iOS - How to call NSInteger variable in button title?

I've been trying to change button title through variable value but I am not getting it.
NSInteger value1=4;
[butttonIcon setTitle:#"value1" forState:UIControlStateNormal];
Here the code you wrote, actually setting "Value1" as the title of your button. If you want to display the NSInteger variable as your button title, then first of all you need to convert the NSInteger to NSString, because the intake argument for button's title is NSString.
So first convert theNSIntegertoNSString`, then use the string as your button's title. You can do that by following ways.
NSInteger value1=4;
NSString *title = [NSString stringWithFormat:#"%d", value1];
[butttonIcon setTitle:title forState:UIControlStateNormal];
You can not set integer value directly to button title text.
You have to first convert it to NSString and than set it to button title text.
like,
NSString *string = [NSString stringWithFormat:#"%#", value1];
[butttonIcon setTitle:string forState:UIControlStateNormal];
So now it is NSString value and it will display your value1 as title.
Hope this will help you.
Assuming in your case you want the title to be 4, just use this for the string:
[#(value1) stringValue]

Apple Watch get button title on IBAction

I tried all the options for iPhone, to get the title of a button on click, but none of them works on Watch Simulator.
So far, I tried:
• NSString *senderTitle = [sender titleForState:UIControlStateNormal];
• NSString *title = [(UIButton *)sender currentTitle];
• UIButton * button = (UIButton *)sender;
NSString *title = [[button titleLabel] text];
• UIButton *button = (UIButton *)sender;
NSString *title = button.currentTitle;
Any suggestions, please?
WatchKit doesn't include a sender with button actions and even if it did there is no way to get the current title from a WKInterfaceButton. If you need to know the title of the button you should have a different action for each of your buttons so you know which button was pressed to cause the action.
If you want to change the title of a button on a tap in WatchKit it is very simple.
-(IBAction)changebuttontitle:(id)sender {
[self.button setTitle:#"button title here"];
}
In WatchOS2 this is
buttonOutlet.setTitle("David Bowie")
and also note that you cannot change the button title color. For that you will need to create a an identical button with a custom title color in PS then swap the image using
setBackgroundImage(image: UIImage?)

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

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;

Better way to get button setTitle?

Currently I am doing this:
NSInteger senderTag = [sender tag];
But all I really want is the title/value of the button pressed not the tag id.
You can get the text of your button's label by checking
NSString *txt = [sender titleLabel].text;
This is not as reliable as the tag, though, especially when your app changes the text on the label in response to user interactions or due to selection of a new locale.
Also note that to set the label you need to use a different method:
[sender setTitle:#"Hello" forState:UIControlStateNormal];

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