iOS - How to call NSInteger variable in button title? - ios

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]

Related

Get value from Button

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

extract UIButton object from NSString

I want to hide already created UIButton object of same name as contents of string myBtnName
NSString *str=#"first";
NSString *myBtnName=[str stringByAppendingString:#"Btn"];
myBtnName is (NSString ) and has value of #"firstBtn"... How do i make it (UIButton) firstBtn ?... Please Help
don't under stand your question properly but i get your problem little bit. that you want to set title of button by appending your variable try this:
this will append your text i.e #"Btn" with your variable: hope this helps.. :)
UIButton *btn = [btn setTitle:[NSString stringWithFormat:#"%#Btn",yourVariableString] forState:UIControlStateNormal];
UIButton *button = [button setTitle:[NSString stringWithFormat:#"%#Btn",yourVariableString] forState:UIControlStateNormal];
You question is unclear to me, but if you want to get a button that you defined as a property by it's name you can use this:
NSString* myBtn = [str stringByAppendingString:#"Btn"];
UIButton* button = [self valueForKey:myBtn];

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;

Accessing buttons in a table using accessibility labels

I need a way to get a reference to each button in the calendar so I can alter its background image whenever I want. I want to do:
UIButton *button = [get_reference_to_button_using_Accessibility_label];
[button setBackgroundImage:image]
When I initialize the buttons, I set the buttons' accessibility labels. Can I use them to get a reference to each button and change its background color?
for (NSUInteger index = 0; index < self.daysInWeek; index++) {
NSString *title = [self.dayFormatter stringFromDate:date];
NSString *accessibilityLabel = [self.accessibilityFormatter stringFromDate:date]; // I can probably use it to refer to this button??
[self.dayButtons[index] setTitle:title forState:UIControlStateNormal];
[self.dayButtons[index] setAccessibilityLabel:accessibilityLabel];
NSLog(#"day buttons!! %#", self.dayButtons[index]);
...
Could you use date to generate a unique (for your calendar) integer in your initializer, and then use that to set a tag on the view?
NSInteger myTag = [date timeIntervalSince1970];
[self.dayButtons[index] setTag:myTag];
Then when you want to reference the button, all you need is its associated date:
UIButton *myButton = (UIButton *)[self.view viewWithTag:[date timeIntervalSince1970]];

How to change the string stored in the NSMutableDictionary itself which is pointed by many pointers?

I have a string in the format str=value|value|value|value|... and it is stored in in an MutableDictionary I create as much as buttons as the number os string value in the string above by converting the string into array using
[str componentsSeparatedByString:#"|"]
and set that whole string to the title of the button.Below is the code I have explained above
[myMutableDictionary setObject:str forKey:date];
NSArray *events=[str componentsSeparatedByString:#"|"];
CGRect rect1=CGRectMake(0, 0,scheduleScroll.bounds.size.width, TIMEROWHWIGHT);
for (NSString *strInfo in events) {
if([strInfo boolValue])
{
NSArray *arr=[strInfo componentsSeparatedByString:#":"];
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
rect1.size.height=TIMEROWHWIGHT*[[arr objectAtIndex:1] intValue];
[btn setTitle:[myMutableDictionary objectForKey:date] forState:UIControlStateApplication];
// add the Button as a sub view and define a method and target
the Buttons I've added will take the string I've added and change it and again set the changed string as the new vale for the same key (I keep a track on the keyValue currently worked on) in the selector I added for touchupInside controlEvent
My problem is that the changed string is not get reflected in the title property for UIControlStateApplication for all buttons I've added eventhough they have a pointer to the value of the key in that dictionary
Try replacing the UIButton creation code with this code :
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
rect1.size.height=TIMEROWHWIGHT*[[arr objectAtIndex:1] intValue];
[btn setTitle:[NSString stringWithFormat:#"%#",[myMutableDictionary objectForKey:date]] forState:UIControlStateNormal];
Hope it will help you :)
Use UIControlStateNormal instead of UIControlStateApplication unless you are specifically changing the state of the button to anything else but UIControlStateNormal (default).

Resources