change image on click of UIButton in objective c [duplicate] - iphone

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
change image on click of button in objective c
i draged 3 button in my .xib file and initially i given default image to them using property window as first.png.
i don't know name of my buttons, because i created button by dragging, i want to know what will be the name of my button ?
now when user clicks on firstButton, image of button should change from first.png to second.png..
and when user selects on 2nd button, image of button2 should change from first.png to second.png, and also change image of 1st button to default again first.png, so that user came to know he has clicked 2nd button.
in short i want to implement like radio button.
how should i do this ?
Thanks In Advance !!

There are multiple ways in which you can do this..
here are a couple of those..
You can give name to these buttons using IBOutlets and determine which button sent the message.
You can determine by tag on buttons which can be edited on the xib itself and you can determine which button sent the message using the tag properties..

As Ankit explian you this can done in multi ways
I suppose you have created two button add_btn, requestBtn with Interface Builder.
Now you need to set the connection (Event, outlet) for both button.
I think you have knowledge so, I will not explain here how to set button (control) connection (Event, outlet) via inteface builder.
For example: add_btn connected to AddFriends methods and requestBtn with getFriendRequest.
Now you can change the Image of Button As
#pragma mark change Image of first Button
-(void)AddFriends:(UIButton*)sender{
[add_btn setImage:[UIImage imageNamed:#"Cliked1.png"] forState:UIControlStateNormal];
[requestBtn setImage:[UIImage imageNamed:#"unCliked2.png"] forState:UIControlStateNormal];
}
#pragma mark change Image of second Button
-(void)getFriendRequest:(UIButton*)sender{
[add_btn setImage:[UIImage imageNamed:#"unCliked1.png"] forState:UIControlStateNormal];
[requestBtn setImage:[UIImage imageNamed:#"Cliked2.png"] forState:UIControlStateNormal];
}
I hope it will help you

Related

Click event for edit/delete button when tableview is in edit mode

I am showing my my table view in edit mode. So for all rows on left side of table view, there are delete button and by clicking on that it changes its image and showing "Delete" button on right side. I want click event for button available at left side. Please help.
If you are using your custom button as a subview then you can addTarget to your button like this:
[button addTarget:self action:#selector(yourMethod) forControlEvents:UIControlEventTouchUpInside];
Please be specific or show some screenshots. So that I can figure it out.
You cannot achieve this without subclassing UITableViewCell. Please review this answer.

How to open a view with a button clicked as default

In my app i want to open a view with the content of a particular button (so that button should look clicked and should be not clickable). I have 4 button with pictures and all the four have different content inside them (Table view with different content).When this view gets open i want the first button clicked automatically and the content of that button should get displayed and by clicking any other button the content of that button should get displayed and the previous clicked button should be available to click again.
I am using different pictures for clicked and unclicked button.
Thanks,
Maybe this will help you
- (void)didClickButton:(id)sender {
UIButton *optionButton = (UIButton *)sender;
if(lastSelectedButton.tag!= optionButton.tag) {
optionButton.selected = YES;
//According to your needs enable or disable the button's interaction
}
Here lastSelectedButton should be an instance variable.
What you're describing sounds like a segmented control. Essentially the segmented control works like buttons on a tape recorder (dating myself, I know.) When you press Play, it stays down and can't be pressed again until you press Stop or FF or Rew, etc. (Ok, Stop doesn't really work that way, but the rest of the buttons do)
Unfortunately, I don't believe you can use your own images in a UISegmentedControl, but fortunately there's an open-source version that should work for you: https://github.com/xhan/PlutoLand/blob/master/PLSegmentView.h
Once you have the control in place you can change the content of your main view depending on the value of the segmented control. You can handle that in the UIControlEventValueChanged event
Keep a single selector for all the buttons something like
[btn addTarget:self action:#selector(templateSelected:) forControlEvents:UIControlEventTouchUpInside];
and make use of the tag to carry any index to the selector
[btn setTag:integer];
and if you want to keep track of previously clicked button then keep a global (id) and assign the current button address to the that id.
And if you want the first button to be clicked on load then call the function melodramatically during initialization of the first button.
[self templateSelected:firstButton];

slider gallery with touch recognition

HI
i was following this example to create a slider gallery
"http://lievendekeyser.net/index.php?module=messagebox&action=message&msg_id=1351"
But i stuck at one point. what i am trying to do is, when user double tap on current image, it should navigate to another view, like to view B.
But i am not able to navigate and detect double tap on current image.
suggestions needed.
regards
you can find the number of tap in touch event. and when count of tap == 2 then you should write code for further movements.
else put all these images in UIButton and provide the IBActions to all the buttons and perform the action.
use following code
[ScrollViewFavButton addTarget:self action:#selector(FavClickButton:) forControlEvents:UIControlEventTouchUpInside];
ScrollViewFavButton.tag = i;
[ScrlViewFavorite insertSubview:ScrollViewFavButton atIndex:i];
insert button in the scrollview and perform action in FacClickButton:.
I think now you'll get your answer.

changing background image of button?

HI ALL,
I have created two custom buttons using IB and i have set their background image.Now i want that when a user selects a button its background image should change and the new background image should persists until user presses the other button.how to do this?
You have to manage the states of your buttons by yourself in this case.
Meaning that you should hold a BOOL member for each button that will state if the button is selected.
Or, if you should have only one selected button in a time, then you might hold a reference to the selected button.
In the tap events you should manage the states above by changing the image of the last selected button to non-selected image and the image of the currently selected button to selected button image.
You can change the image like this:
[button setBackgroundImage:[UIImage imageNamed:#"selected_button.png"] forState:UIControlStateNormal];
You can combine 2 methods to do so:
- (void)setImage:(UIImage *)image forState:(UIControlState)state
call it using [self.button setImage:YOUR_IMAGE forState:UIControlStateSelected];
Then you can set the button to be selected. [self.button setState:UIControlStateSelected] . When another button is selected, you set the state back to normal.

button inside table view cell shows highlighted state on tapping on cell

I have an issue with the background image of button which is dependent of button state. I have a button inside customized table view cell and i have set different images for button's different states. Please look into the code below.
[btnNow setImage:[UIImage imageNamed:#"now_norm.png"] forState:UIControlStateNormal];
[btnNow setImage:[UIImage imageNamed:#"now_focus.png"] forState:UIControlStateHighlighted];
Whenever i tap on the actual button this works great but if i tap on area outside of button but which is still inside that same cell then this button changes its background image from UIControlStateNormal to UIControlStateHighlighted.
If i remove background image from UIControlStateHighlighted state then this issue doesn't exists but i need pressed state of the button.
Please help me out.
Thanks in advance!
Vivek Dandage.
Try with setting the cell.selectionStyle to UITableCellSelectionStyleNone
I wonder how to comment on karim's Answer. But that should be marked as right. It is the answer to the problem. I know, because I was having the same problem and setting the selectionStyle of the cell to None was the solution.