Accessing buttons in a table using accessibility labels - ios

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]];

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]

Make changes to a number of buttons programatically

I have a view with 10 buttons and I want to use information from an array to set how many of the buttons are visible and what their title is.
The buttons are named option1BTN...option10BTN. The array has different data and size depending on what the user selects and I want to buttons to also reflect the changes.
The code below shows a for-do loop, that sets which button is visible and the button title
for (int i=0; i == [optionsArray count]; i++) {
self.option1BTN.hidden = NO;
[self.option1BTN setTitle:[optionsArray objectAtIndex:i] forState:UIControlStateNormal];
}
How can I change the button name (programatically) in the loop so that depending on the size of the array it changes to option1BTN then option2BTN...optionXBTN and so on?
NSArray *buttons = #[self.option1BTN, self.option2BTN]; // add all the buttons here
for (int i=0; i < buttons.count; i++) {
UIButton *button = buttons[i];
button.hidden = NO;
[button setTitle:[optionsArray objectAtIndex:i] forState:UIControlStateNormal];
// the previous line can be re-written as
//[button setTitle:optionsArray[i] forState:UIControlStateNormal];
}
The difference between this approach and using an IBOutletCollection (as Jeff suggests in his answer) is that the outlet collection does not guarantee the order of its items. If the order is important to you, you need to specify it yourself, like in my code snippet above.
You have two main options, one is to use an IBOutletCollection (if your buttons are created in Interface Builder). Each UIButton will be stored in the collection and you can retrieve the right button in the loop from the collection.
Example:
#property (nonatomic, strong) IBOutletCollection(UIButton) NSArray *buttons;
In IB, you will need to link each button to the IBOutletCollection. Then in your code you will be able to do:
for (int i = 0; i < optionsArray.count; i++) {
UIButton *tempButton = self.buttons[i];
tempButton.hidden = NO;
[tempButton setTitle:[optionsArray objectAtIndex:i] forState:UIControlStateNormal];
tempButton.hidden = NO;
}
The ordering of an IBOutletCollection is not always guaranteed so you may need to use a combination of IBOutletCollection and view tagging (see end of post). For more information on IBOutletCollection's see NSHipster's article
Second option is if your buttons are created programmatically, store each button in an NSMutableArray as you create it:
#property (nonatomic, strong) NSMutableArray *buttons;
UIButton *myButton;
// Do something...
[self.buttons addObject:option1BTN];
Then use the same loop as before.
A third (not recommended option), would be to tag each UIButton and then you can use the viewWithTag: method to retrieve the button. E.g. using the above code again but getting the button with:
UIButton *tempButton = [self.view viewWithTag:i];

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];

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).

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];

Resources