Get text of button from IBAction - iPhone - iphone

When an IBAction is called:
-(IBAction) onClick1: (id) sender;
What is passed in the sender? Since it's hooked up through the IB, I'm not really sure. My question is how to get the text of the button to be the passed object (NSString most likely) so that I could call it inside the action implementation.
-(IBAction) onClick1: (id) sender {
NSLog(#"User clicked %#", sender);
// Do something here with the variable 'sender'
}

The sender should be the control which initiated the action. However, you should not assume its type and should instead leave it defined as an id. Instead, check for the object's class in the actual method as follows:
- (IBAction)onClick1:(id)sender {
// Make sure it's a UIButton
if (![sender isKindOfClass:[UIButton class]])
return;
NSString *title = [(UIButton *)sender currentTitle];
}

It's actually:
-(IBAction) onClick1: (id) sender {
NSLog(#"User clicked %#", sender);
// Do something here with the variable 'sender'
}
sender is not a NSString, it's of type id. It's just the control that sent the event. So if your method is trigged on a button click, the UIButton object that was clicked will be sent. You can access all of the standard UIButton methods and properties programmatically.

-(IBAction)onClick:(id) sender {
UIButton *btn = (UIButton *)sender;
//now btn is the same object. And to get title directly
NSLog(#"Clicked button: %#",[[btn titleLabel] text]);
}

Simply write the following code
-(IBAction) getButtonTitle:(id)sender
{
UIButton *button = (UIButton *)sender;
NSString *buttonTitle = button.currentTitle;
NSLog(#"Button Title %#",buttonTitle);
}
Thats it... you have done!!!

Sender should be defined as type id, not int or NSString. The sender is the actual object that's calling the method; if you hooked it up to a button, it will be a UIButton, if it's a text field, a UITextField. You can use this to get information from the control (for example the text field's current string value), or compare it to an IBOutlet instance variable if you have multiple controls hooked up to the same action method.

You can just use the following to get the button label and determine which one was clicked:
NSLog(#"Clicked button: %#",[[sender titleLabel] text]);
To answer your question, the id is the object from the IB.

To fetch the text from the button:
NSLog(#"Date::%#",[btn titleForState:UIControlStateNormal]);

Related

Button value copy to other button

How to get the pressed button title text value and to shows as other pressed button if pressed?i have used the following code to get the title text from button
- (IBAction) checkIt:(id)sender
{
UIButton *button = (UIButton *)sender;
NSLog(#"Button text value %#", button.titleLabel.text);
}
How to show the button text into other button if i pressed? Please help me to resolve this
Do like this,
- (IBAction) checkIt:(id)sender
{
UIButton *button = (UIButton *)sender;
[your_other_button setTitle:button.titleLabel.text forState:UIControlStateNormal];
}
you need to Create Globle Variable at your app-delegate like:-
yourAppdelegate.h
#property (strong, nonatomic) NSString *buttonTitleCopy;
yourAppdelegate.m
#synthesize buttonTitleCopy;
now you just get this variable at your Particular Class like:-
yourClass.h
#import "yourAppdelegate.h"
//create Object of your Delegate Class
yourAppdelegate *objAppdelegate;
yourClass.m
- (void)viewDidLoad
{
objAppdelegate = (REMAppDelegate *) [[UIApplication sharedApplication]delegate];
[super viewDidLoad];
}
now in your Method:-
- (IBAction) checkIt:(id)sender
{
UIButton *button = (UIButton *)sender;
objAppdelegate.buttonTitleCopy=button.titleLabel.text
NSLog(#"Button text value %#", delegate.buttonTitleCopy);
}
now you can use buttonTitleCopy variable anywere in your project with app-delegate class Object. Hope your getting this and you solve your issue.
NOTE:- just set your OtherButton Title with
otherButton.titleLabel.text=objAppdelegate.buttonTitleCopy;
You can try this way
- (IBAction) checkIt:(id)sender
{
otherButton.titleLabel.text=sender.titleLabel.text;
}
you can get it to point tags or just make a variable button and add it to your button. And get btn text.

UIButton clicked passing parameters?

is there a way to pass a boolean to the addClicked method beside using button.tag for the code below?
[cellview.buttonAdd addTarget:self action:#selector(addClicked:) forControlEvents:UIControlEventTouchUpInside];
-(void) addClicked:(id)sender {
}
THanks in advance.
if you want to add a integer property , you can use tag.
if you want to add a nonInteger property , you must use a category with Associative References,the inheritting UIButton can not post property at all.
you can see this :
Subclass UIButton to add a property
Try something like this:
-(void) addClicked:(id)sender
{
UIButton * button = (UIButton*)sender;
NSLog(#"Button Tag: %i", button.tag);
}
Not sure what you mean by pass Boolean.
Short answer: You cannot pass extra information into the method directly.
Why would you want to do that anyway though? What does the button "know" that it would need to communicate, other than the fact that it was clicked?
The way this should be done is via an instance variable in the class that implements the click handler.
If you really must maintain state inside the button itself, subclass it:
#interface CustomButton : UIButton
#property (nonatomic, assign) BOOL myBoolValue;
#end
/* ... */
- (void)addClicked:(id)sender
{
CustomButton *button = (CustomButton *)sender;
if (button.myBoolValue) {
// Whatever you want to do.
}
}

Refreshing UILabel

I have an IBAction that displays what is being pressed on certain buttons to a UILabel. I have another IBAction for equalPressed button that does many things, but I also added titleDisplay.text = nil; It works perfectly the first time. Then after I press the equalPressed button it doesn't show up. I know it is because i have titleDisplay.text set to nil. However, I don't know how to clear the UILabel with the equalPressed button so my other buttons can be displayed on the screen without appending constantly
First IBAction
- (IBAction) titleLabel: (UIButton *) sender {
NSString *titleOfButton = [[sender titleLabel] text];
titleDisplay.text = [[titleDisplay text] stringByAppendingString: titleOfButton];
}
Second IBAction
- (IBAction) equalPressed: (UIButton *) sender {
titleDisplay.text = nil;
}
The reason why this would work only the first time is because when you assign nil to an object, you're essentially dumping your reference to it. You should instead set the text to an empty string, like so:
[titleDisplay setText:#""];
Try
- (IBAction) equalPressed: (UIButton *) sender {
titleDisplay.text = #"";
}

iphone-Identifying container of a button

I have a class like this,
#interface event
{
NSString *name;
UIButton *button;
}
In my view controller i am having several objects of event class, and i added all the buttons of those event objects to the view controllers class(self.view)
-(void) buttonPressed:(id) sender
Now i am handling the touchevent in my view controller as given above, now how can i identify the event object to which the pressed button(sender) belongs to.
You can simply compare the button property of the event object to the sender parameter of your action method. Assuming that your event objects are simply member variables of your view controller, your buttonPressed code might look something like this:
- (void)buttonPressed:(id)sender {
if (sender == event1.button) {
NSLog(#"Button pressed for event1");
}
else if (sender == event2.button) {
NSLog(#"Button pressed for event2");
}
}
Obviously you'll have to declare button as a #property of your Event object.
You should add a tag to your objects like:
myObject.tag = 1;
Then, in your buttonPressed method do something like
if(sender.tag == 1){
//...doSomething
}

iPhone: NSLocalizedString in interface builder?

Is there any way to insert an NSLocalizedString in interface builder.
For example set a label text to a localized string instead of a static string?
I really hate to create a property for every single item that requires a localized string.
This post might have some tips for you:
http://blog.wilshipley.com/2009/10/pimp-my-code-part-17-lost-in.html
Even if this post is old, for those interested in automatically localizing your IB files, check this out: https://github.com/angelolloqui/AGi18n
DISCLAIMER: I am the developer of the library
You can take advanced of the User Defined Runtime Attributes:
http://cupobjc.blogspot.com.es/2014/04/interfaz-builder-localization.html
First define a new category for UILabel:
#import "UILabel+Localized.h"
#implementation UILabel (Localized)
-(void) setTextLocalized:(NSString *)aText{
[self setText:NSLocalizedString(aText, nil)];
}
#end
Then in the interface builder, User Defined Runtime Attributes :
textLocalized String your string to localized
To avoid creating a bunch of categories, create just one that categorize the NSObject and then check for the isKindOfClass as suggested. See the code below:
#import "NSObject+Localized.h"
#implementation NSObject (Localized)
///
/// This method is used to translate strings in .xib files.
/// Using the "User Defined Runtime Attributes" set an entry like:
/// Key Path: textLocalized
/// Type: String
/// Value: {THE TRANSLATION KEY}
///
-(void) setTextLocalized:(NSString *)key
{
if ([self isKindOfClass:[UILabel class]])
{
UILabel *label = (UILabel *)self;
[label setText:NSLocalizedString(key, nil)];
}
else if ([self isKindOfClass:[UIButton class]])
{
UIButton *button = (UIButton *)self;
[button setTitle:NSLocalizedString(key, nil) forState:UIControlStateNormal];
}
else if ([self isKindOfClass:[UIBarButtonItem class]])
{
UIBarButtonItem *button = (UIBarButtonItem *)self;
[button setTitle:NSLocalizedString(key, nil)];
}
}
#end
NSLocalizedString is not the recommended way to localize Interface Builder files. Check out ibtool:
http://www.bdunagan.com/2009/03/15/ibtool-localization-made-easy/
I have done same thing as #OAK mentioned. Here is full code.
Interface Builder Localization HowTo