Button value copy to other button - iphone

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.

Related

Get UIButton reference from viewDidLoad

I am very new to iPhone development. I am trying to disable an already existing button but I cant actually obtain a pointer to a specific element in the view. For instance, I have the following in the viewController header
- (IBAction)one:(id)sender;
and the implementation is
- (IBAction)one:(id)sender {
}
which are just event handlers. However, I need disable the button when view opens and I am a little bit lost on how to obtain references to elements outside of the event handler.
So in other words, my thought is to have something like:
UIButton* myButton = //something
where the something is where I am lost on what to do. Any ideas? I greatly appreciate any help I get on here!
You need to create a property for your button in the interface:
#property(nonatomic, retain) IBOutlet UIButton * button;
And add this to implementation:
#synthesize button;
Then connect the button to it in interface builder. After this you can disable the button by:
button.enabled = NO;
Hope I could help!
Just give tag to your button and access your button with tag value.
UIButton *btn = (UIButton*)[self.view viewWithTag:1];
[btn setHidden:YES];
In your .h File
#import <UIKit/UIKit.h>
#interface RpViewController : UIViewController
#property (retain , nonatomic)IBOutlet UIButton *Btn1;
#end
In your .m file , in implementation write this :
#synthesize Btn1;
Now on interface , click on button.
In button's properties - > Drawings - check Hidden checkbox.
Wherever you want to show that button , just write.
[Btn1 setHidden:FALSE];
#property (strong, nonatomic) UIButton *button;
#synthesize button;
// In View Did Load...
self.button = [UIButton buttonWithType:UIButtonTypeCustom]; // button can be of any type.
[self.button setTag:1];
// if you have more buttons initialize it and set its tag. you can get to know which button was pressed using tags.
[button addTarget:self action:#selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
-(void) buttonEvent:(UIButton *) sender
{
NSLog(#"%d",sender.tag);
if(sender.tag == 1)
{
[self.button setEnabled:NO]; // This makes your button disabled, i.e you can see the button but you cannot click on it.
[self.button setHidden:YES]; // This makes your button hidden.
}
}
if you have more doubts ping me back.

IBOutletCollection of UIButtons wont set highlighted

I have an IBOutletCollection of UIButtons:
#property (nonatomic, retain) IBOutletCollection(UIButton) NSMutableArray *Buttons;
with an ibaction i would to change the highlighted state permanently after the touch down event.
This Problem is very similar to this:
IBOutletCollection of UIButtons - changing selected state of buttons
... but with the for-loop the buttons doesnt change.
i also tried the perfomselector method from here: Keep iPhone UIButton Highlighted
but it doesnt work.
now my code:
-(IBAction)toggleButtons:(id)sender
{
NSUInteger Index = [button tag];
[[Buttons objectAtIndex:Index] setHighlighted:YES];
}
if i change line four to this:
[[Buttons objectAtIndex:3] setHighlighted:YES];
it works for the fourth element in my collection... But not with the index variable....
regards, phil
Update
SelectionViewController.h
#import <UIKit/UIKit.h>
#interface SelectionViewController : UIViewController
#property (nonatomic, retain) IBOutletCollection(UIButton) NSMutableArray *Buttons;
- (IBAction)toggleButtons:(id)sender;
#end
SelectionViewController.m
#import "SelectionViewController.h"
#interface SelectionViewController ()
#end
#implementation SelectionViewController
#synthesize Buttons;
-(IBAction)toggleButtons:(id)sender
{
UIButton *button = sender;
NSUInteger Index = [button tag];
[self performSelector:#selector(doHighlight:) withObject:sender afterDelay:0];
[[Buttons objectAtIndex:Index] setHighlighted:YES];
}
- (void)doHighlight:(UIButton *)b {
[b setHighlighted:YES];
}
Okey Update 2:
Now i had declared my Buttons as normal IBOutlet and this is not working:
-(IBAction)toggleButtons:(id)sender
{
UIButton *button = sender;
[button setHighlighted:YES];
}
But if change it to this:
-(IBAction)toggleButtons:(id)sender
{
[myOutletButton setHighlighted:YES]; //Normal Outlet
}
it works....
But why is not possible with the sender?
regards!
Update 3
This works also:
for(id button in self.view.subviews)
{
[button setHighlighted:YES];
}
Ok if change the delay time in the selector to 1, the state will be highlighted. I am using "touch down" event... i think after i touched up the button gets its old state. Which event is the right?
Given that your example works with a specific integer, the problem is probably that the tag property is not set properly for each of your buttons. If the buttons are created in interface builder, each of them will have a default tag value of 0. To check this, click on the button and then, in the Attributes Inspector, scroll down to View and see what value is entered in the tag field

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 = #"";
}

how to get image name from an UIButton on iPhone sdk?

I'm trying to get the name of the background image from a custom button.
I call this method when I click over the UIButton:
-(void)getButtonImageName:(id)sender{
UIButton *resultButton = (UIButton *)sender;
NSLog(#" The button's image is %#.", resultButton.currentImage);
}
I can get the title and other properties from the button but not the background image name.
Anyone know how to do that? Thanks!
This is the simplest approach to get the current image of a custom button
-(void)yesButtonButtonCliked:(id)sender
{
UIButton *tmpYes = (UIButton *)sender;
[self ClearAllButtons:tmpYes.tag:#"YES"];
UIImage* selectedImg=[UIImage imageNamed:#"checkboxtick.png"];
if (tmpYes.currentImage == selectedImg )
{
}
else
{
[tmpYes setImage:[UIImage imageNamed:#"checkboxtick.png"] forState:UIControlStateNormal];
}
NSLog(#"Button tag is %d",tmpYes.tag);
}
There is no method to turn an UIImage into the name used with [UIImage imageNamed:].
do you abuse the image to store data?
What do you try to accomplish with the name of the image?
Edit:
I have several buttons, I want to get the image name when I press a button, switch to another view controller and load that image with the name.
No need to do this, just pass the UIImage instance.
If you want to show a different image based on the name, like you have a thumbnail and want to show the big version in another view controller you have to get the image name from your data model and pass it to the view controller.
How do you decide which button gets which image?
Following on Matthias's answer "No need to do this, just pass the UIImage instance," which was the solution for me, with the specific code for fellow newbs:
Calling .m:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
//Create an instance of the destination controller
dmImageZoom *dmz = (dmImageZoom *)[segue destinationViewController];
//Get the tapped button (the image was in a button) into a button object
UIButton *btnImg= sender;
//Get the button's image object
UIImage *img=btnImg.currentImage;
//Pass the image object to the destination controller's instance variable
dmz.imgPassed=img;
Called .h:
#interface dmImageZoom : UIViewController {
//Instance variable
UIImage *imgPassed;
}
//Property for the object that presents the image
#property (strong, nonatomic) IBOutlet UIImageView *imgView;
//Property that enables it to be assigned a value
#property (strong, nonatomic) IBOutlet UIImage *imgPassed;
Called .m:
- (void)viewDidLoad
{
...
imgView.contentMode = UIViewContentModeScaleAspectFit;
//Assign the image to view object
imgView.image=imgPassed;
#fluchtpunkt As you say, I have some thumbnails that are UIbuttons with a background image. What happens is that I'm creating as follows for the category selected in the previous view controller. Something like this...
if( [strSection isEqualToString:#"animals"] ){
[button_thumbnail_1 setBackgroundImage:[UIImage imageNamed:#"image1.png"] forState:(UIControlState)normal];
[button_thumbnail_2 setBackgroundImage:[UIImage imageNamed:#"image2.png"] forState:(UIControlState)normal];
}
if( [strSection isEqualToString:#"cars"] ){
[button_thumbnail_1 setBackgroundImage:[UIImage imageNamed:#"image1.png"] forState:(UIControlState)normal];
[button_thumbnail_2 setBackgroundImage:[UIImage imageNamed:#"image2.png"] forState:(UIControlState)normal];
}
}
Thanks for response
You could inherit UIButton:
//.h
#interface MySpecialButton : UIButton {
int mySpecialAttribute;
}
#property (nonatomic) int mySpecialAttribute;
- (void) setBackgroundImage;
- (UIImage*) getImage;
#end
And use your special attribute to both set and return the image name:
//.m
#implementation LetterButton
#synthesize mySpecialAttribute;
- (void) setBackgroundImage {
[self setBackgroundImage:[self getImage] forState:UIControlStateNormal];
[self setBackgroundImage:[self getImage] forState:UIControlStateDisabled];
}
- (UIImage*) getImage {
NSString *fileName = [NSString stringWithFormat:
#"My_special_image_%i.png", mySpecialAttribute];
return [UIImage imageNamed:fileName];
}
#end
Set the class of the button in the xib:
UIButton *senderButton = (UIButton*)sender;
if ([senderButton.currentTitle isEqualToString:#"Hello"]) {
[senderButton setTitle:#"H" forState:UIControlStateNormal];
}
else {
[senderButton setTitle:#"Hello" forState:UIControlStateNormal];
forState:UIControlStateNormal];
}
iOS 11 Swift 4.2
#objc func btnWishListAddRemove(_ sender:UIButton){
let index = sender.tag
let product_id = array_packageList[index].product_id!
if sender.currentImage == UIImage(named: "wish list"){
sender.setImage(UIImage(imageLiteralResourceName: "wishlistfill"), for: .normal)
}
else if sender.currentImage == UIImage(named: "wish list fill"){
sender.setImage(UIImage(imageLiteralResourceName: "wishlist"), for: .normal)
}
}

Connecting Multiple Buttons to one action?

I have multiple UIButtons in my app. I also use interfacebuilder. In my .h i have something like this
IBOutlet UIButton *button1;
IBOutlet UIButton *button2;
IBOutlet UIButton *button3;
- (IBAction)buttonPressed;
Then In my m i want to do something like this
- (IBAction)buttonPressed {
if (theButtonIpressed == button1)
{
// do something if
}
}
The problem is I don't have something called "theButtonIpressed" so I cant do this. What should my if statement look like? I don't want to make a -(IBAction) for each button. Is there something that I can determine which button was pressed? Thanks!
Thanks,
-David
You can also set the tag property for each button in the interface builder and then use it to find out which button was pressed.... This also means that you don't need to define all your button references (UIButton) and keep track of them in code....
- (void) doSomething:(id)sender {
int buttonPressed = [sender tag];
switch (buttonPressed) {
case 0:....
// etc
}
}
Define your - (IBAction)buttonPressed to:
- (IBAction)buttonPressed: (UIButton *) buttonIpressed
Then it will work.
- (IBAction)buttonPressed:(UIButton*)button
But if you're doing something different for each button then the proper way to do it is to create separate IBActions.
You can use tag values for each buttons
IBOutlet UIButton *button1;
button1.tag = 100;
IBOutlet UIButton *button2;
button2.tag = 200;
IBOutlet UIButton *button3;
button3.tag = 300;
- (IBAction)buttonPressed:(id)sender
{
if ([sender tag]==100)
{
NSLOG("button1");
}
else if([sender tag]==200)
{
NSLOG("button2");
}
else {
NSLOG("button3");
}
}