Apple Watch get button title on IBAction - ios

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

Related

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

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 can create a UIButton with Hebrew language text?

Problem:
Text (Hebrew language) which I set in button shows conversely.
For example:
NSString *title = #"עמוד הבית"; //This is right-to-left
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:title forState:UIControlStateNormal];
so:
In my simulator i see:
//This is left-to-right
How can it fix?
How can I show text correctly?
App supported iOS 6 and iOS 7
What worked for me when using an UIButton was this:
if ([UIView userInterfaceLayoutDirectionForSemanticContentAttribute:button.semanticContentAttribute] == UIUserInterfaceLayoutDirectionRightToLeft)
{
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
}
Links worth reading:
Supporting Right-to-Left Languages (Apple Reference)
AutoLayout + RTL + UILabel text alignment
if you use "#x200F" that will solve your problem,add this as appended string.There is one issue is after adding it all come with and that's weird,if you know the answer.Please, share

AutoResizing a button based on title text content

So I have a strange issue which could be a bug or me being stupid.
I have a button in a cell. The cell shows the logged in users facebook friends. If they are already a user of the app it shows the button as Play, else it shows Invite Me...
UIButton *playButton = (UIButton *)[cell.contentView viewWithTag:30];
playButton.backgroundColor = [UIColor clearColor];
[playButton setBackgroundImage:[[UIImage imageNamed:#"fbCell_Invite_30h"] resizableImageWithCapInsets:UIEdgeInsetsMake(15, 5, 15, 5)] forState:UIControlStateNormal];
playButton.titleLabel.text = #"Invite me";
if ([self.fbFriendsUsingApp containsObject:[friend valueForKey:#"id"]]) {
playButton.titleLabel.text = #"Play";
}
This all works fine and not an issue. However, when the button text is invite me it shows as this:
In storyboard interface builder the button is setup like this:
What is strange is that if I click the middle arrow <------> to make the width stretch, the title text stops working and they all show 'Play' ???
Try setting the text of the button using
[playButton setTitle:#"Invite Me" forState:UIControlStateNormal];
instead of
playButton.titleLabel.text = #"Invite Me;
I've found that if I don't use the first approach above the changes to the button text don't show up.
You can find the size of the NSString that you are setting as the button label and then resize the button accordingly:
CGSize labelSize = [labelString sizeWithFont:[UIFont systemFontOfSize:12]];
[button setFrame:CGRectMake(10,0,labelSize.width, labelSize.height)];
You will probably want to add a little to the CGSize as padding, but play with it and find the result you desire.

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