I need a UIButton to act as a Radio button (selection button). When the user
clicks on the button, the color should change (to indicate that the user
clicked this button), and when the user clicks the same button again (the already selected
button) the color of the button should change to the default color indicating
that the button was un-selected.
How can i do this programatically ?
There might be other alternatives to do this, but i require to use the button
to do this.
You can specify different images for selected and normal button states:
[btnName setImage:selectedImage forState:UIControlStateSelected];
[btnName setImage:normalimage forState:UIControlStateNormal];
And then in button's action method simply toggle button's state between normal and selected:
- (void) buttonAction:(UIButton*)sender{
sender.selected = !sender.selected;
}
Put the below code in your button action.
if([btnName isSelected]) {
[btnName setImage:#"SelectedImage" forState:UIControlStateNormal];
[btnName setSelected:NO];
} else {
[btnName setImage:#"deSelectedImage" forState:UIControlStateNormal];
[btnName setSelected:YES];
}
Hope it will be useful.
Follow below link:
Radio Button in iPhone
Related
I want to be able to change the right navigation bar button to a different button with name and action.
As an Example:
btnOpenImage is pressed and rightNavButton1 changes to rightNavButton2
-(IBAction) btnOpenImage_Clicked:(id)sender{
//1. IF buttons are UIBarButtonItem then use bellow code
// This bellow line for Change the action(Target)
[rightNavButton1 setAction:#selector(rightNavButton2_Clicked)];
//This bellow line For Change the Title
[rightNavButton1 setTitle:#"rightNavButton2_Clicked"];
//OR 2. IF buttons are UIButton then use bellow code
// This bellow line for Change the action(Target)
[rightNavButton1 addTarget:self action:#selector(rightNavButton2_Clicked) forControlEvents:UIControlEventTouchUpInside];
//This bellow line For Change the Title
[rightNavButton1 setTitle: #"rightNavButton2" forState: UIControlStateNormal];
}
Try this:
[btnOpenImage addTarget:self
action:#selector(aMethod)
forControlEvents:UIControlEventTouchDown];
- (void)aMethod
{
[rightNavButton1 setTitle: #"rightNavButton2" forState: UIControlStateNormal];
}
I have 2 button objects (UIButton *obtn,*ebtn), I have created 12 buttons(6 buttons with 6 tag values for obtn, 6 buttons with 6 tag values for ebtn)... I have a single btnClick: method for all those 12 buttons... when i click on a button of tag value 1... Iam getting the properties of selected button to another button by using sender... I want to get the properties of unselected button (Example obtn object with tag value 3) into another button How can i get it...?
My Direct Question is: How to assign the properties of a button with a tag value to another button when it is in unselected state...
It sounds like you want to be able to get a pointer to a button with a certain tag, and at the moment all you can do is get the button that has sent you the action.
The method you are looking for is viewWithTag:. This will get you a button (or any other view) with the tag you want. You pass this method to the superview of the button. So, in your action method (assuming all buttons are in the same superview):
UIView *superview = [sender superview];
UIButton *otherButton = [superview viewWithTag:3];
You then have sender, which is the button that was tapped, and otherButton, which is the button with tag 3. You can modify each one as you wish.
// Create textfield 5
btn_select_Country = [UIButton buttonWithType:UIButtonTypeCustom];
//btn_select_Client = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn_select_Country.frame = CGRectMake(BTN_X_CORD, 228, BTN_WIDTH, BTN_HEIGHT);
btn_select_Country.tag = 1;
// [btn_select_Client setTitle:#"Button One" forState:UIControlStateNormal];
[btn_select_Country addTarget:self action:#selector(countryListBtnTap:) forControlEvents:UIControlEventTouchUpInside];
btn_Image_Select_Country= [UIImage imageNamed:#"drop-down.png"];
[btn_select_Country setImage:btn_Image_Select_Country forState:UIControlStateNormal];
//btn_select_Client.tag=1205;
[scrollview addSubview:btn_select_Country];
- (IBAction)currencyListBtnTap:(id)sender;
{
button_Number = [sender tag];
[self allTextFeildResignFirstResponder];
}
Is it possible to make the navigationcontroller title a button? but without a button look to it. I want it to be just text that if you click on it a view/actionsheet pops up from the bottom
If so, can you show me programmatically how I would make it a button?
Thanks
You can set TitleView of navigationItem to set a button as title.
Here is what i tried and got it working:
UIButton*button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame=CGRectMake(0, 0, 30, 20);
[button setTitle:#"Testing" forState:UIControlStateNormal];
[button addTarget:self action:#selector(btnAction) forControlEvents:UIControlEventTouchUpInside];
[[self navigationItem] setTitleView:button];
But if you don't want it to look like a button, you have set the buttonWithType to custom and set its backGroundImage property.
in btnAction have your code to show the pop ups.
I am having trouble with the behavior of one of my UIButtons. I am trying to essentially make it a toggle button, but I am running into the problem below.
I have the code:
UIButton *likeButton = [[UIButton alloc] initWithFrame:CGRectMake(horizontalOffset+buttonWidth, verticalOffset, buttonWidth, buttonHeight)];
[likeButton setImage:[UIImage imageNamed:#"like-off.png"] forState:UIControlStateNormal];
[likeButton setImage:[UIImage imageNamed:#"like-on.png"] forState:UIControlStateHighlighted];
[likeButton setImage:[UIImage imageNamed:#"like-on.png"] forState:UIControlStateSelected];
[likeButton addTarget:self action:#selector(likeButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
which fires the method:
-(void)likeButtonPressed:(id)sender {
UIButton *button = (UIButton *)sender;
[button setSelected:!button.selected];
}
The behavior I am seeing is that when I tap the button down (and highlight it), it works as expected, and the 'like-on.png' image is used for the highlighted state, and it remains on in the 'selected' state.
However, when I tap the button again, to toggle it off, I see a gray highlighted state when I press my finger. When I release my finger, I see the 'like-off' image is shown as expected.
I would like to avoid seeing the gray highlighted state when I press my finger down on the button when I go to toggle it off. Instead I would like to make sure that the highlighted state on toggle-off uses the 'like-on.png' image as specified in the code.
What's going on here? Any ideas where my code could be incorrect?
Many thanks,
Brett
You're missing the image for the selected and highlighted state:
[likeButton setImage:[UIImage imageNamed:#"like-on.png"] forState:UIControlStateSelected | UIControlStateHighlighted];
If you don't set it, the image of the normal state is used. From the -[UIButton setImage:forState:] documentation:
In general, if a property is not specified for a state, the default is to use the UIControlStateNormal value.
If you don't want your images to be modified when they are highlighted, set:
likeButton.adjustsImageWhenHighlighted = NO;
I think the selected property of UIButton is meant for something different (think of the desktop UI).
It would be more consistent to change the for all states according to a BOOL that tracks if it is "on" or "off".
Thus,
-(void)likeButtonPressed:(id)sender {
UIButton *button = (UIButton *) sender;
liking = !liking;
if (liking) {
// configure the four states with "like-on" and other images
}
else {
// configure the four states with "like-off"
}
}
Otherwise you would use the state of a UI element to represent your program logic, which is basically flawed. The only instance where this is sort of acceptable (but not really) is a UISwitch.
alt text http://img216.imageshack.us/img216/2288/problem11.png
As given in the picture, I have an search bar in my application.
Now I want to handle my custom action on Cancel Taped.
-> On Cancel Taped -> PopViewController
-> On Cancel Taped -> Clear Search Text & Hide Search Bar Keyboard.
I don't know how to handle Cancel Tap?
Thanks in advance for helping me.
Ok ! I tried Something Different. I added a new button on Cancel Button.
UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
btn.backgroundColor=[UIColor clearColor];
[btn setFrame:CGRectMake(260, 5, 53, 32)];
[btn addTarget:self action:#selector(onCancelSearch:) forControlEvents:UIControlEventTouchUpInside];
[searchBar addSubview:btn];
Now. See Following image. but it is with RoundedRect Type, Instead Use TypeCustom - so it will become invisible.
alt text http://img269.imageshack.us/img269/4337/problem12e.png