Change UIbutton Image on click - objective-c

I have stored 5 images in an mutable array and displayed them randomly on iPhone view by appending them in UIButton . now I want to change the image on a button on which I will click. but in my code only the last image changes not the image on which I called the action.

Here is the code for 3 buttons which highlights the clicked button
-(void) changeButtonImage:(id) sender{
[button1 setBackgroundImage:[UIImage imageNamed:#"button1Image_off.png"] forState:UIControlStateNormal];
[button2 setBackgroundImage:[UIImage imageNamed:#"button2Image_off.png"] forState:UIControlStateNormal];
[button3 setBackgroundImage:[UIImage imageNamed:#"button3Image_off.png"] forState:UIControlStateNormal];
UIButton *button = sender;
if (button.tag == 0) {
[button1 setBackgroundImage:[UIImage imageNamed:#"button1Image_on.png"] forState:UIControlStateNormal];
}else if (button.tag == 1) {
[button2 setBackgroundImage:[UIImage imageNamed:#"button2Image_on.png"] forState:UIControlStateNormal];
}else if (button.tag == 2) {
[button3 setBackgroundImage:[UIImage imageNamed:#"button3Image_on.png"] forState:UIControlStateNormal];
}
}
hope this helps...
hAPPY cODING...

If you are looking for how to change the image on a button just do:
[myButton setImage:[UIImage imageNamed:#"myImage.png"] forState:UIControlStateNormal];
You can have it loop through your array but creating a variable to store what image index you are on. Then just go to the next one using the statement above to assign the image.

Do you have an Outlet (IBOutlet) for each of the buttons. You should list each one as an outlet, and just use Interface Builder to connect each button to those variables. Then create a function for the touchUpInside event of each button. Make this buttonPressed. make this function something like:
-(void) buttonPressed:(id) sender
{
((UIButton *)sender).image = [UIImage imageNamed:#"Image.png"];
}
You will want to set a variable like currentImage to track what image is set. Each time you click increase that variable (currentImage++). If it gets > some final amount set it back to 0. Then you can just do
if (currentImage == 0) { set first image; } else if (currentImage == 1) { set second image.. }
etc...
Does this help?

Related

Button click not registering

I have an Xcode 5 button problem.
I create a number of button programatically on a UIViewController and then want to register clicks on these buttons. I use NSLog to track the clicks but it seems that no clicks are registered in the log.
Can anyone help please?
The ultimate aim to to segue to next UIViewController on each button click, but was just testing to see if button clicks were registered first.
Here is how I create the buttons in viewDidLoad (only 1 shown).
NOTE: the if-statement used only to check which buttons were pressed in previous view.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *myString = [NSString stringWithFormat:#"%d", self.tagNumber ];
if (self.tagNumber == 1)
{
hotelButton = [UIButton buttonWithType:UIButtonTypeCustom];
hotelButton.tag = 1;
hotelButton.frame = CGRectMake(60, 60, 300.f, 100.f);
UIImage *buttonImage1 = [UIImage imageNamed:#"buttonImage1.png"];
[hotelButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
[self.view addSubview:hotelButton];
// Add targets and actions
[hotelButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpOutside];
}
}
And here is how I check for button clicks
- (void) buttonClicked
{
NSLog(#"Button %i clicked", hotelButton.tag);
}
NOTE: I also tried the following:
- (void) buttonClicked:(UIButton *) button
{
NSLog(#"Button %ld clicked", (long int)[button tag]);
}
But that gave an error "undeclared selector buttonClicked" in my viewDidLoad pointing to action#selector):
[hotelButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpOutside];
Use this:
[hotelButton addTarget:self action:#selector(buttonClicked)
forControlEvents:UIControlEventTouchUpInside];
From Apple Docs:
UIControlEventTouchUpInside
A touch-up event in the control where the finger is inside the bounds of the control.
UIControlEventTouchUpOutside
A touch-up event in the control where the finger is outside the bounds of the control
And keep this method:
- (void) buttonClicked
{
NSLog(#"Button %i clicked", hotelButton.tag);
}
try and change outside to inside,
[hotelButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
Try this,
[hotelButton addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
and pass the sender,
- (void) buttonClicked:(UIButton*)sender
{
NSLog(#"Button %i clicked", [sender tag]);
}
Have modified your if condition. Please follow :-
if (self.tagNumber == 1)
{
hotelButton = [UIButton buttonWithType:UIButtonTypeCustom];
hotelButton.tag = 1;
hotelButton.frame = CGRectMake(60, 60, 300.f, 100.f);
UIImage *buttonImage1 = [UIImage imageNamed:#"buttonImage1.png"];
[hotelButton setBackgroundImage:buttonImage1 forState:UIControlStateNormal];
hotelButton.showsTouchWhenHighlighted = YES;
hotelButton.imageEdgeInsets = UIEdgeInsetsMake(10, 10, 10, 10);
[hotelButton addTarget:self action:#selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:hotelButton];
}

UIButton show selected state on tap

How do I show the selected state for the UIButton on tap?
I want it so that when people tap on the uibutton it shows the selected state with the hover that I have?
Right now they have to hold the button to see the hover.
[t1 setBackgroundImage: [UIImage imageNamed: [NSString stringWithFormat: #"hover.png"]] forState:UIControlStateSelected];
I want to show the hover image on tap also.
You need to set the selected state for the button
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setImage:[UIImage imageNamed:#"hover.png"] forState:UIControlStateSelected];
To show a hoover image while the button is pressed you need to set the state property to UIControlStateHighlighted
I had the same problem - my button was highlighted when pressed (tap+hold) and wasn't - when simply tapped. Here is my code:
btn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *image =[UIImage imageNamed:#"btn"];
UIImage *imageSelected =[UIImage imageNamed:#"btn_tap"];
[btn setBackgroundImage:transImage forState:UIControlStateNormal];
[btn setBackgroundImage:imageSelected forState:UIControlStateHighlighted];
[btn addTarget:self action:#selector(action:) forControlEvents:UIControlEventTouchUpInside];
-(void)btnPressedWithDelay{
//your code for btn action
}
- (void) action:(UIButton *)sender{
[self performSelector:#selector(btnPressedWithDelay) withObject:nil afterDelay:0.1];
}
Did it work for you?

UIImage Buttons (Rounded Rect Button)

I want to be able to change the image of any of the buttons upon the user pressing the button and then when they release, the image that was there before is put back.
So basically when they press their finger on the button, the image changes and when they release, the original image I specified in IB is put back.
How can I do this? Here is the code I have so far.
- (IBAction)btnOne:(id)sender {
result.text = [self appendresult:#"1"];
//Changes the button image upon touch
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1.png"] forState:UIControlStateNormal];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateHighlighted];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateSelected];
btnOne.showsTouchWhenHighlighted = YES;
}
Put your code...
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1.png"] forState:UIControlStateNormal];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateHighlighted];
[btnOne setImage:[UIImage imageNamed:#"NumberBtn1Alt.png"] forState:UIControlStateSelected];
btnOne.showsTouchWhenHighlighted = YES;
in viewDidLoad instead of in the IBAction, then just make sure that your button in Interface Builder is properly linked to the IBOutlet you created for it.

how to change the button image inside the table cell while clicking the button in iphone

I have a tableview in my application. I created a button inside the table cell and assigned a background image to it. I wanted to change the background image when i click that button. I assigned an action for the button like this
-(void)goodBtnClicked:(id)sender {
[goodBtn setBackgroundImage:[UIImage imageNamed:#"camera.png"] forState:UIControlStateNormal];
}
But, the image is not getting replaced with the new image. Can any one help me with this?
You should make sure that your image is actually there.
-(void)goodBtnClicked:(id)sender {
UIImage *img = [UIImage imageNamed:#"camera.png"];
NSAssert(img, #"Image shouldn't be nil");
[goodBtn setBackgroundImage:img forState:UIControlStateNormal];
}
try with
-(void)goodBtnClicked:(id)sender {
[sender setBackgroundImage:[UIImage imageNamed:#"camera.png"] forState:UIControlStateNormal];
}
Go through all the subviews and locate your button then change background image
- (void)buttonPressed:(UIButton *)sender{
UITableViewCell *cell = (UITableViewCell *)sender.superview;
for (UIView *sub in cell.subviews) {
if([sub isMemberOfClass:[UIButton class]])
{
UIButton * button=[[UIButton alloc] init];
button=sub;
[button setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
}
}
}

Help with iphone button pressed

I am trying to see which button was clicked on so I can preform the correct logic.
This is the code for the buttons:
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(270, 423, 60, 60)];
[button addTarget:self action:#selector(buttonPressedAction:)
forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:[[UIImage imageNamed:#"refreshicon.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0]
forState:UIControlStateNormal];
button.tag = 1;
UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 423, 60, 60)];
[button2 addTarget:self action:#selector(buttonPressedAction:)
forControlEvents:UIControlEventTouchUpInside];
[button2 setBackgroundImage:[[UIImage imageNamed:#"login.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0]
forState:UIControlStateNormal];
button2.tag = 2;
[self.navigationController.view addSubview:button];
[self.navigationController.view addSubview:button2];
And this is how I call the buttonPressedAction:
- (void)buttonPressedAction:(id)sender
{
UIButton* button = (UIButton*)sender;
if(button.tag == 1)
{
NSLog(#"1");
}else
{
NSLog(#"2");
}
}
But when I use NSLog to see what the sender value is, it crashes.
Any advice for what's happening and how to correct it?
Now corrected :o) THANKS!
As other have pointed out, UIButton does not have a value property. I guess you are trying to detect which button was clicked. Here are two ways to do this:
Use the tag property one each button. I.e. button1.tag = 1, button2.tag = 2. You can then test which button was clicked with if(sender.tag == 1) etc. You could introduce constants for the numbers to make the code more readable.
If you keep a reference to the button, you can check if the reference is equal. Example: if(sender == self.button1)
Probably the best approach would be to give buttons that do different things different actions, but failing that, you could distinguish them by tag.
- (void)buttonPressedAction:(id)sender
{
UIButton* button = (UIButton*)sender;
// do something
}
If you were saving the pointers to the button objects you are creating, you could compare the pointers. Or you can just set the tag property for the button, and use that to switch behaviour.