extract UIButton object from NSString - ios

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

Related

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

iOS: I set my uibuttons with tags. How do I change the specific button image based on its tag?

I have a multiple array of buttons called button. Each one is tagged. How do I change the image on the button based on its tag and tag only. As of right now, it only changes the very last button.
-(void)buttonTapped:(id)sender{
NSLog (#"%i",[sender tag])];
[button setImage:[UIImage imageNamed:#"button_change.png"] forState:UIControlStateNormal];
}
Either:
for (UIButton *btn in button) {
if(btn.tag == 1)
{
// do something
break; // don't need to run the rest of the loop
}
}
if you want to use the array (it shouldn't be called 'button', use something with a plural for an array)
or an easier way:
UIButton *btn = (UIButton *)[self.view viewWithTag:1];
However a much simpler way would be to use the param in the callback (unless thats not the button you want). Like so:
-(void)buttonTapped:(id)sender
{
UIButton *tappedBtn = (UIButton *)sender;
[tappedBtn setImage:[UIImage imageNamed:#"button_change.png"] forState:UIControlStateNormal];
}
If you just want to change the button that was tapped the following should work.
-(void)buttonTapped:(id)sender
{
NSLog (#"%i",[sender tag])];
UIButton *tappedButton = (UIButton *)sender;
[tappedButton setImage:[UIImage imageNamed:#"button_change.png"] forState:UIControlStateNormal];
}
If you want to change other buttons then you can retrieve the buttons using
[self.view viewWithTag:1000]; //1000 is the tag you assigned
You don't really need to use tags in the this case. When an IBAction is invoked, the sender parameter is a pointer to the control that triggered the IBAction. (Your button.)
Thus, you already have a pointer to the button.
So, as others have pointed out, your code could read like this:
-(void)buttonTapped:(UIButton *)sender
{
[sender setImage:[UIImage imageNamed:#"button_change.png"] forState:UIControlStateNormal];
}
Note that I changed the type of sender to UIButton so that you don't have to cast it. As long as the action is only ever connected to a button, this is safe to do and makes the code cleaner.
As the other poster pointed out, having an array of buttons called "button" is bad. I renamed it to "buttons" in the code below:
If you wanted to do it using tags and an array of buttons, you could use code like this:
-(void)buttonTapped:(UIButton *)sender
{
NSUInteger tag = [sender tag];
UIButton *aButton = buttons[tag];
[aButton setImage:[UIImage imageNamed:#"button_change.png"] forState:UIControlStateNormal];
}
Create images with names button_change1.png, button_change2.png, etc.
And:
-(void) buttonTapped:(UIButton*)sender
{
NSString* imageName = [NSString stringWithFormat: #"button_change%ld.png", (long)sender.tag];
[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
}

How to set button image by variable?

i am trying to set the button image by variable like this:
[collideButton setImage:[UIImage imageNamed:#"key%#.png",[getKeyboarLabel]] forState:UIControlStateNormal];
this is not working an error comes expected identifiers! i dont know where do i missed anything.
if i set image by this code:
[collideButton setImage:[UIImage imageNamed:#"keya.png"] forState:UIControlStateNormal];
it works good..
now please tell me where is the problem in uper code?
use [NSString stringWithFormat#"key%#.png", key];
NSString *imageFilename = [NSString stringWithFormat:#"key%#.png", getKeyboarLabel];
[collideButton setImage:[UIImage imageNamed:imageFilename] forState:UIControlStateNormal];

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

How to Change textLabel property in UIButton programmatically in iOS?

I have a UIButton that should have 2 statuses - "Play" and "Pause"; i.e. - at the first time the user sees it it says "Play" and then whenever the user clicks it it should switch between "Play" and "Pause".
I managed to create the controller itself - the content is played and paused properly - but I cannot seem to change the UIButton Text Label text.
I use:
myButton.titleLabel.text = #"Play";
myButton.titleLabel.text = #"Pause";
It's not working. The text is not changing. I also tried [myButton.titleLabel setText:#"Pause"] and it's not working as well.
How can I set it?
It should be:
[myButton setTitle:#"Play" forState:UIControlStateNormal];
You need to pass the state as well. You can check other state's here.
You can then do something like this:
[myButton setTitle:#"Play" forState:UIControlStateNormal];
[myButton setTitle:#"Stop" forState:UIControlStateSelected];
SWIFT 4
myButton.setTitle("Pause", for: .normal)
I found this to be somewhat out of date since attributed strings were added. Apparently titles of buttons assigned in a storyboard are attributed strings. Attributed titles take precedence over NSString titles, so if you want to use a simple string as a title you have to remove the attributed title first. I did as below, though there may be a better way. You could, of course, make your new title also an attributed string.
[myButton setAttributedTitle: nil forState: 0xffff];
[myButton setTitle: #"Play" forState: UIControlStateNormal];
And if you want to load it on page load, and support localized language:
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (self.flagToChangeLabel){
NSString* newBtnTitle = NSLocalizedString(#"LOCALIZED_KEY", nil);
[self.laterButton setTitle:newBtnTitle forState:UIControlStateNormal];
[self.laterButton setTitle:newBtnTitle forState:UIControlStateSelected];
}
}

Resources