Changing the Second Button Image by Clicking the First Button IOS7 - ios

Hi I'm trying to change the button image by clicking the another button for example i have two buttons like button1 and button2 already this two button has images but now I'm trying to change the second button image by clicking the first button in second button the new image should replace the old image please tell me how to do it.
- (IBAction)bt1:(id)sender {
UIButton *btn = (UIButton*)sender;
[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
}
- (IBAction)bt2:(id)sender {
}
**[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"]** forState:UIControlStateNormal];
This above code is used to change the button image but i want know how to change the second button image by clicking image.
Thanks

You might have the IBAction property of a second button, which you can access in your implementation (.m) file with self (e.g. self.secondButtonPropertyName), now you can write the code like below:
-(IBAction)bt1:(id)sender
{
[self.btn2 setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
}

first you set your both button tag..like btn1 tag =1 and btn2 tag =2
- (IBAction)bt1:(id)sender {
UIButton *btn = (UIButton*)[self.view viewWithTag:2];
[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
}
- (IBAction)bt2:(id)sender {
}

suppose you have two buttons btn1 and btn2
Now after them allocatng memory,
you can use below Button selector,
btn1 selector,
-(IBAction)btn1selector:(id)sender
{
[btn2 setbackgroundimage: [uiimage imagenamed:#"your image"]];
}
you can do same for the other button selector.

Related

Change image on button click in ios

I have four buttons on a view controller..ViewControllerA.On click of ecah button a viewcontroller in container view opens.I want when button A is clicked then the image on button A changes and image on button B,C,D remains same .when button B is clicked then the image on button B changes and image on buttonA,C,D remains same.
when button C is clicked then the image on button C changes and image on buttonA,B,D remains same.when button D is clicked then the image on button A changes and image on buttonA,B,C. remains same
- (IBAction)AClick:(id)sender {
[_A_btn setImage:[UIImage imageNamed:#“Ahover.png"] forState:UIControlStateNormal];
[_B_btn setImage:[UIImage imageNamed:#“B.png"] forState:UIControlStateHighlighted];
[_C_btn setImage:[UIImage imageNamed:#“C.png"] forState:UIControlStateSelected];
[_D_btn setImage:[UIImage imageNamed:#“D.png"] forState:UIControlStateSelected];
__A_btn.showsTouchWhenHighlighted = YES;
}
I ma following this code .kindly suggest the changes required as it is not working as desired.
first set images for buttons for required states as well, and u can change state of the button on click as follows , it will show the image of that state.
- (IBAction)AClick:(UIButton *)sender {
sender.selected = !sender.selected;
}

UIButton with UIControlEventTouchDown, image won't change to selected until finger lifted

Trying to get my head around this. I have a UIButton with a UIControlEventTouchDown event that calls:
- (IBAction)pressButton:(id)sender {
NSLog(#"button pressed");
UIButton *button = (UIButton *)sender;
button.selected = YES;
}
I am getting the ,message "button pressed" instantly as expected. However the image won't change to the one of selected until I lift my finger. This doesn't make sense as I am setting the button to selected as soon as the button is pressed. I have my off image set in the interface builder for "default" and my on image set under "selected". I have tried also adding an image in for "highlighted" and this also doesn't work. What am I doing wrong here?
You have setup your button clicked event on UIControlEventTouchDown so the event will fire on TouchDown event of the button and you want to set the image on that so you have to set image's UIControlState.
so try to set image like that :
[button setImage:[UIImage imageNamed:#"pressed.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
that might help you.
You can set the highlighted image for your button. In storyboard its like this:
try with this
- (IBAction)pressButton:(id)sender {
NSLog(#"button pressed");
UIButton *button = (UIButton *)sender;
button.selected = YES;
[button setBackgroundImage:[UIImage imageNamed:#"another.png"] forState:UIControlStateNormal];
}

when i click a button in ios app i want the image to change

the title much explains it really in my button
- (IBAction)LikeBtn:(id)sender
when the button is clicked i want ti to swap images from a small grey icon to a green icon
im trying it this way but i cant seem to get it to work
- (IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage :#"liked.png" forState:UIControlStateSelected];
{
Has Any body any ideas how this can be done at the moment the image is selected in the xcode control panel and is named likebtn.png and need it changing to liked.png after clicked
Assuming you have the image properly added to your project, and it's a png:
- (IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:#"liked"] forState:UIControlStateSelected];
}
PS: Are you sure you wish to set the image for the selected state? If not, use UIControlStateNormal instead.
I think you need to setImage in viewDidLoad when creating button:
[LikeBtn setImage :[UIImage imageNamed:#"liked.png"] forState:UIControlStateSelected];
In addition to what Maddy says about passing a UIImage rather than a string for a selected state, you will also need to declare that the button is now selected. If you change your code to this, it should work for you
-(IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:#"liked.png"] forState:UIControlStateSelected];
senderButton.selected = YES;
}
You can also put a if (senderButton.selected) condition within the IBAction method to detect and toggle between button states, changing the state from YES to NO.
Hope this helps

Changing button image when another button is clicked

I'm facing a problem in xcode, in one of my view controllers i have two buttons, when one of them is clicked (appleBtn) the second button's (topMenu1Btn) image should change
I created an outlet for both buttons, an action for the applebtn when clicked
- (IBAction)HandleBtnClick:(id)sender {
self.topMenu1Btn.imageView = [UIImage imageNamed:#"Apple.jpg"]
}
the thing is the imageview is read-only
is there anyway to do this?
- (IBAction)HandleBtnClick:(id)sender
{
[self.topMenu1Btn setBackgroundImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal];
}
You could try this to set the image for the button.
[self.topMenu1Bt setImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal]
If you want the background image use setBackgroundImage: instead
[self.topMenu1Btn setBackgroundImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal];
Or
[self.topMenu1Bt setImage:[UIImage imageNamed:#"Apple.jpg"] forState:UIControlStateNormal]
If you use the second method then make sure that the frame of image matches with the button
Make sure that Button type is Custom
(IBAction)HandleBtnClick:(id)sender {
self.topMenu1Btn.imageView.image = [UIImage imageNamed:#"Apple.jpg"]
}
You can't modify the imageview, but you can modify the image in it ;)

Setting an UIImage for UIControlStateHighlighted

board[i] is an array of UIButtons that I have created programmmatically, and I can't change their image for UIControlStateHighlighted:
[board[i] setImage:[UIImage imageNamed:#"block"] forState:UIControlStateNormal];
[board[i] setImage:[UIImage imageNamed:#"blockPressed"] forState:UIControlStateHighlighted];
When I press the button with the mouse in the simulator the image doesn't change. I think this is a very noob question, but I don't what the code doesn't work.
when adding button programatically do this:
add target of each same.
provide tag all button from 0 to count.
set UserInteraction to true
setBackgroundImage:[UIImage imageNamed:#"blockPressed.png"] forState:UIControlStateHighlighted if u want button to be highlited
Now button is pressed same method is called for all button: For example
-(void)ButtonTouched:(id)sender
{
UIButton *btntouched = sender;
NSLog(#"%#", btntouched);
[btntouched setBackgroundImage:[UIImage imageNamed:#"blockPressed.png"] forState:UIControlStateHighlighted];// it can be forState:UIControlStateNormal also
}
I don't think you are triggering the highlighted state. This could be because they are not set to have interaction enabled. Or there is something else missing from the way you set up your buttons.
The other thing you can try is to add a selector to each of the buttons for when they are touched, and then change the image by referencing sender for the selector function.
Assuming you've made sure your image doesn't return nil, this code should work:
[myUIButton setImage:[UIImage imageNamed:#"myHighlightedButtonImage.png"] forState:UIControlStateHighlighted];
It should work find if you call that line in your viewDidLoad().

Resources