changing background image of button? - iphone

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.

Related

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

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

UIButton sets dark when it is selected (I want to avoid this)

I'm developing an iOS 4 application.
I'm using a custom uibutton to make an image clickable. When user taps over the image, it will disappear.
It's not very pretty to see that the image gets black, then turns to its original color, and then disappear.
Is there a way to disable that effect?
You will need to set the property adjustsImageWhenHighlighted to NO:
[button setAdjustsImageWhenHighlighted:NO];
Alternatively you can set the same image for all controlStates of the button.
You need to set the property Shows Touch On Highlight to enabled.
Programmatically you can do that with:
[button setShowsTouchWhenHighlighted:YES];
Since it's a custom button, you get to specify the image you want to show when it's highlighted. Create the image you want to show in that situation.
If you are using interface builder just set the Highlighted and selected states to the same image as the default image.

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];

Change (not init) a UIBarButtonItem identifier programmatically?

In IB I can set the identifier of a UIBarButtonItem to 'play' which adds an image of a play button (right-pointing triangle).
Is there a way to change this image programmatically? I want to change it to 'pause' when the play button is pressed.
I know you can initialize a UIBarButtonItem with an identifier but I've yet to find a way to change it after it's been initialized. Is this even possible?
The only thing I can think of is to remove the old button and initialize a new one in its place, but this hardly seems efficient.
Any thoughts?
Ok I've googled this question to death and ran into sample code from Apple where they do exactly the same thing (toggle play/pause button graphic on a toolbar button). But instead of using the built in play and pause identifiers of UIBarButtonItem they use a custom UIButton and toggle custom images.
So if Apple goes through the trouble of creating and toggling custom images on a UIButton instead of the built in play and pause UIBarButtonItem buttons then I think it's pretty safe to say there's no way to programatically change the identifier of a UIBarButtonItem.
This is what they (Apple) do to toggle the images when the button is pressed:
// Call this when the button you want to toggle is pressed:
[playButton setImage:((p.playing == YES) ? pauseBtnBG : playBtnBG) forState:UIControlStateNormal];
Replace p.playing with whatever BOOL you want to hold the state of your button. playButton is the custom UIButton in the toolbar. pauseBtnBG and playBtnBG are the images to toggle.
This seems to work fairly well:
UIBarButtonItem *oldButton = [myToolBar.items objectAtIndex:1];
[myToolBar setItems:[NSArray arrayWithObjects:[myToolBar objectAtIndex:0], [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:self action:#selector(tapStopRefreshButton:)],nil] animated:NO];
[oldButton release];
In this example I had a toolbar for a UIWebView and when someone clicked Reload I wanted it to change to Stop. The toolbar only had a flexible space and the one button on it - to right-align the button - so I grabbed a reference to the old button, made a new one with the same selector as the old, reset the buttons on the tab bar, and then released the original button.
Not the prettiest, but you can use all standard buttons without having to override the button class(es).
What about 2 stacked toolbars? Then you can have some system buttons in the top one, and others in the bottom one. If the play button is pressed, then just hide the top toolbar.

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.