Set only single option in iOS - ios

I have two custom round rect button and on this did tap event on image show.Like When we select gender there is only one option to select other automatically hide.but my gives when i touch first button than it select but when i touch second button no operation select.
My main moto is that i want to choose gender but in iOS no radio button use so i design custom and on click event set image.
calling api is a string type.
-(void)viewdidload
{
button = [UIButton buttonWithType:UIButtonTypeCustom];
callApi=#"MALE";//calling Api is a string type//
[button addTarget:self action:#selector(roundButtonDidTap:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)roundButtonDidTap:(UIButton*)tappedButton
{
if([callApi isEqualToString:#"FEMALE"]) {
UIImage *btnImage = [UIImage imageNamed:#"bullet.png"];
[button setImage:btnImage forState:UIControlStateNormal];
}
if ([callApi isEqualToString:#"MALE"]) {
UIImage *btnImage = [UIImage imageNamed:#"bullet.png"];
[button2 setImage:btnImage forState:UIControlStateSelected];
button.imageView.hidden=YES;
}
}

try this code:
-(IBAction)selectMale:(id) sender{ // MALE BUTTON
UIButton *RadioButton1 = (UIButton*)sender;
[self radiobuttonAction:RadioButton1];
}
-(void)radiobuttonAction:(UIButton *)Button
{
if(![Button isSelected])
{
[Button setSelected:YES];
[Button setImage:[UIImage imageNamed:#"radio_active.png"] forState:UIControlStateSelected];
gender = #"Male";
[btnFemale setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateSelected];
[btnFemale setSelected:NO];
}
else
{
[Button setSelected:NO];
[Button setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateNormal];
}
}
-(IBAction)selectFemale:(id) sender{
UIButton *RadioButton1 = (UIButton*)sender;
[self radiobuttonActionFemale:RadioButton1];
}
-(void)radiobuttonActionFemale:(UIButton *)Button
{
if(![Button isSelected])
{
[Button setSelected:YES];
[Button setImage:[UIImage imageNamed:#"radio_active.png"] forState:UIControlStateSelected];
gender = #"Female";
[btnMale setImage:[UIImage imageNamed:#"radio.png"] forState:UIControlStateSelected];
[btnMale setSelected:NO];
}
else
{

Related

how to check three button conditions

I am having three button image, I want to check the conditions between them. when I click the button 1 means background image change to blue colour. at the same time I click the button 1 it will move to normal state white colour. Same for another two buttons.For that process i am using this code. [tempBtn setSelected:! tempBtn.selected];
ButtonImageSelected = [UIImage imageNamed:#"lblue.png"];
ButtonImage = [UIImage imageNamed:#"l.png"];
button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.frame = CGRectMake(80, 27, 36, 36);
[button1 setBackgroundImage:ButtonImage forState:UIControlStateNormal];
button1.tag = 1;
[button1 setBackgroundImage:ButtonImageSelected
forState:UIControlStateSelected];
button1.userInteractionEnabled = YES;
[button1 addTarget:self
action:#selector(button1Selector:)
forControlEvents:UIControlEventTouchDown];
[cell.contentView addSubview:button1];
Another two buttons are same as button 1.
My condition is: when button 1 is clicked means button 3 should not change. when button 3 is clicked means button 1 should not change.button 2 can select in both the condition.I am using below code to check the conditions but its not working correct.
- (void)select_id:(UIButton *)tempBtn {
if (tempBtn.tag == 1) {
button3.userInteractionEnabled = NO;
[tempBtn setSelected:!tempBtn.selected];
} else if (tempBtn.tag == 3) {
button1.userInteractionEnabled = NO;
[tempBtn setSelected:!tempBtn.selected];
}
}
help me to how to check the conditions.In the above method.
I believe what you are trying to accomplish is to have each button change states as you press them. Once you have defined your buttons, this is how you can ensure they alternate images for each state.
Alternating Images Based on Button Presses
-(IBAction)buttonPressed:(id)sender {
switch ([sender tag]) {
case 1:
if ([UIImagePNGRepresentation(button1.imageView.image) isEqualToData:UIImagePNGRepresentation([UIImage imageNamed:#"default1. png"])]) {
[button1 setImage:[UIImage imageNamed:#"selected1.png"] forState:UIControlStateNormal];
} else {
[button1 setImage:[UIImage imageNamed:#"default1.png"] forState:UIControlStateNormal];
}
break;
case 2:
if ([UIImagePNGRepresentation(button2.imageView.image) isEqualToData:UIImagePNGRepresentation([UIImage imageNamed:#"default2. png"])]) {
[button2 setImage:[UIImage imageNamed:#"selected2.png"] forState:UIControlStateNormal];
} else {
[button2 setImage:[UIImage imageNamed:#"default2.png"] forState:UIControlStateNormal];
}
break;
case 3:
if ([UIImagePNGRepresentation(button3.imageView.image) isEqualToData:UIImagePNGRepresentation([UIImage imageNamed:#"default3.png"])]) {
[button3 setImage:[UIImage imageNamed:#"selected3.png"] forState:UIControlStateNormal];
} else {
[button3 setImage:[UIImage imageNamed:#"default3.png"] forState:UIControlStateNormal];
}
break;
default:
break;
}
}
Note: make sure you add the action to each button, and change the names of the images
Adding the Actions to Each Button
[button1 addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[button2 addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[button3 addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
Ensure you have done the following:
Added action to each button
Tagged each button accordingly
Set the initial images of each button to match the condition
Try this. Before that make sure the tags are proper for every button
- (void)select_id:(UIButton *)tempBtn {
if (tempBtn.tag == 1) {
button3.userInteractionEnabled = NO;
[tempBtn setSelected:!tempBtn.selected];
} else if (tempBtn.tag == 3) {
button1.userInteractionEnabled = NO;
[tempBtn setSelected:!tempBtn.selected];
}
else{
button1.userInteractionEnabled = YES;
button2.userInteractionEnabled = YES;
[tempBtn setSelected:!tempBtn.selected];
}
}

How to Deselect the image by clicking other button?

Hi in my application I ask the user to provide feedback. This could be to rate our server for example. They do this by clicking one of two buttons, good or bad. When they select an option the image for the button changes which is what I need it to do. The problem I have is that if they select good then select bad the good button needs to revert back to the 'un selected' option. As they cannot bother be 'selected'
My button code.
- (IBAction)gd:(id)sender {
UIButton *btn = (UIButton *)sender;
if ([sender isSelected]) {
[btn setBackgroundImage:[UIImage imageNamed:#"rd.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
}
else{
[sender setBackgroundImage:[UIImage imageNamed:#"rd1.png"] forState:UIControlStateNormal];
[sender setSelected:YES];
}
}
- (IBAction)bd:(id)sender {
UIButton *btn1 = (UIButton *)sender;
if ([sender isSelected]) {
[btn1 setBackgroundImage:[UIImage imageNamed:#"rd.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
}
else{
[sender setBackgroundImage:[UIImage imageNamed:#"rd1.png"] forState:UIControlStateNormal];
[sender setSelected:YES];
}
}
I have used the above but its not working like i want its showing like in the below image.
Please tell me show to resolve this one.
Create an outlet for both buttons and set the images for both states.
- (void)initButtons {
[gdBtn setBackgroundImage:[UIImage imageNamed:#"rd.png"] forState:UIControlStateNormal];
[gdBtn setBackgroundImage:[UIImage imageNamed:#"rd1.png"] forState:UIControlStateSelected];
[bdBtn setBackgroundImage:[UIImage imageNamed:#"rd.png"] forState:UIControlStateNormal];
[bdBtn setBackgroundImage:[UIImage imageNamed:#"rd1.png"] forState:UIControlStateSelected];
}
The actions should then look like these:
- (IBAction)gd:(UIButton *)sender {
[sender setSelected:!sender.isSelected];
[bdBtn setSelected:NO];
}
- (IBAction)bd:(UIButton *)sender {
[sender setSelected:!sender.isSelected];
[gdBtn setSelected:NO];
}
Here is your solution
Add this Function in the starting , to deselect 2 buttons whenever you tap on button:-
-(void)deselectButtons
{
for(UIView *vw in [self.view subviews])
{
if([vw isKindOfClass:([UIButton class])])
{
UIButton *btn=(UIButton *)vw;
btn.selected=FALSE;
}
}
}
for Good Button:-
- (IBAction)bd:(id)sender {
[self deselectButtons];//add this line of code
UIButton *btn1 = (UIButton *)sender;
if ([sender isSelected]) {
[btn1 setBackgroundImage:[UIImage imageNamed:#"rd.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
} else {
[sender setBackgroundImage:[UIImage imageNamed:#"rd1.png"] forState:UIControlStateNormal];
[sender setSelected:YES];
}
}
for BAD Button:-
- (IBAction)bd:(id)sender {
[self deselectButtons];//add this line of code
UIButton *btn1 = (UIButton *)sender;
if ([sender isSelected]) {
[btn1 setBackgroundImage:[UIImage imageNamed:#"rd.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
} else {
[sender setBackgroundImage:[UIImage imageNamed:#"rd1.png"] forState:UIControlStateNormal];
[sender setSelected:YES];
}
}

I want to change the UIButton Highlight Image, but without success

I have a UIbutton in my view
I want to change the UIButton Highlight Image, but without success
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 66, 29)];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_normal.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_selected.png"] forState:UIControlStateHighlighted];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_down_selected.png"] forState:UIControlStateSelected];
[button addTarget:self action:#selector(filterAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
- (void)filterAction:(UIButton *)button
{
if(button.isSelected == YES)
{
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_selected.png"] forState:UIControlStateHighlighted];
button.selected = NO;
}
else
{
#warning I want to change the UIButton Highlight Image, but without success
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_down_normal.png"] forState:UIControlStateHighlighted];
button.selected = YES;
}
}
If you want your button to behave as follows,
normal - filter_button_normal
highlighted (on touch press down on button) - filter_button__down_selected
selected (after removing touch from button) - filter_button_selected;
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_normal.png"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_selected.png"] forState:UIControlStateSelected];
[button setBackgroundImage:[UIImage imageNamed:#"filter_button_down_selected.png"] forState:UIControlStateHighlighted];
so when the user touches it , image changes to highlighted and when he removes his finger over it, it changes to selected
And the target you added is UIControlEventTouchUpInside
[button addTarget:self action:#selector(filterAction:) forControlEvents:UIControlEventTouchUpInside];
so filterAction is called after the user removes his finger from it.
- (void)filterAction:(UIButton *)button
{
// the image will change automatically
if(button.isSelected == YES)
{
button.selected = NO;
}
else
{
button.selected = YES;
}
}

I have to perform togling for single UIButton which I have generated from loop for different frame

In my iOS application, I have to change image as per button click.
I have to perform togling for single UIButton which I have generated from loop for different frame
It is working fine if I perform action on single button, if I have performing action for multiple button the image is not properly changing as per button action
Here is my button
btnFullScreen = [[UIButton alloc] init];
[btnFullScreen setBackgroundImage:[UIImage imageNamed:#"open icon.png"] forState:UIControlStateNormal];
[btnFullScreen setFrame:CGRectMake(self.view.frame.size.width-30,8,16,14)];
[btnFullScreen setBackgroundImage:[UIImage imageNamed:#"close icon.png"] forState:UIControlStateSelected];
[btnFullScreen addTarget:self action:#selector(showPopUp:) forControlEvents:UIControlEventTouchUpInside];
btnFullScreen.autoresizingMask=UIViewAutoresizingFlexibleLeftMargin|UIViewAutoresizingFlexibleBottomMargin;
[self.view addSubview:btnFullScreen];
Here is my button action code
-(void)showPopUp:(id)sender{
if ([sender isSelected]) {
[sender setImage:[UIImage imageNamed:#"open icon.png"] forState:UIControlStateNormal];
[sender setSelected:NO];
}else {
[sender setImage:[UIImage imageNamed:#"close icon.png"] forState:UIControlStateSelected];
[sender setSelected:YES];
}
[self.parent.parent addPopOversExcept:self.parent];
}
Thanks
I had done this before in one of my app.
I was creating button in each tableview cell.
So,Take this code as Reference.
1.Specific Method.
- (void)methodName:(UIButton *)sender
{
int i = [sender.titleLabel.text intValue];
NSNumber *numb;
if(i == 0)
{
numb = [NSNumber numberWithBool:NO];
sender.titleLabel.text = #"1";
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
}
else
{
numb = [NSNumber numberWithBool:YES];
sender.titleLabel.text = #"0";
[sender setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[sender setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
}
}
2.Setting UIButton Programatically in UITabelview viewForHeaderInSection.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(240, 20, 30, 30)];
[button addTarget:self
action:#selector(methodName:)
forControlEvents:UIControlEventTouchDown];
button.tag = section;
if([[sectionsArray objectAtIndex:section] boolValue])
{
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"0";
}
else
{
[button setImage:[UIImage imageNamed:#"down.png"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:#"up.png"] forState:UIControlStateHighlighted];
button.titleLabel.text = #"1";
}

Checkbox control for iOS application

Is there any checkbox control in the object library for iOS applications? The nearest thing I see is the switch control, which can take a boolean value.
You can use the selected state of a UIButton, set different images (or also texts) for differents (via code, or Interface Builder) :
[button setImage:[UIImage imageNamed:#"selected.png"]
forState:UIControlStateSelected];
[button setImage:[UIImage imageNamed:#"unselected.png"]
forState:UIControlStateNormal];
And in the touch up inside action :
- (IBAction)buttonTouched:(id)sender
{
button.selected = !button.selected;
// add other logic
}
//define property of button
Declare Unchecked Image
- (void)viewDidLoad
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
Checked Image.
- (IBAction)buttonTapped:(id)sender
{
if (_checkboxButton.selected == YES)
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateSelected];
_checkboxButton.selected = NO;
}
else
{
[_checkboxButton setBackgroundImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
_checkboxButton.selected = YES;
}
}
This is the code I use for two checkboxes on the screen. (I put them at the bottom of two pictures that are also on the screen. I use the UIControlEventTouchDown to call the checkBoxTapped method. This method talks to my main view controller where I decide if the answer was correct or incorrect. Then the main view controller calls back the this view and tells it to change the blank textbox to either a check:
or an x
// Put the Checkboxes on the screen
UIImage *checkBox = [UIImage imageNamed:#"Checkbox"];
self.checkBoxLeft = [UIButton buttonWithType:UIButtonTypeCustom];
[self.checkBoxLeft setImage:checkBox forState:UIControlStateNormal];
[self.checkBoxLeft setFrame:checkBoxFrameLeft];
[self.checkBoxLeft addTarget:self
action:#selector(checkBoxTapped:)
forControlEvents:UIControlEventTouchDown];
[self.checkBoxLeft setTitle:#"checkBoxLeft" forState:UIControlStateNormal];
self.checkBoxLeft.contentEdgeInsets = insets;
self.checkBoxLeft.showsTouchWhenHighlighted = NO; // Keeps it from turning gray
self.checkBoxRight = [UIButton buttonWithType:UIButtonTypeCustom];
[self.checkBoxRight setImage:checkBox forState:UIControlStateNormal];
[self.checkBoxRight setFrame:checkBoxFrameRight];
[self.checkBoxRight addTarget:self
action:#selector(checkBoxTapped:)
forControlEvents:UIControlEventTouchDown];
[self.checkBoxRight setTitle:#"checkBoxRight" forState:UIControlStateNormal];
self.checkBoxRight.contentEdgeInsets = insets;
self.checkBoxRight.showsTouchWhenHighlighted = NO; // Keeps it from turning gray
- (void)checkBoxTapped:(UIButton *)buttonPressed {
[[self delegate] checkBoxTapped:buttonPressed.currentTitle];
}
- (void)highlightCheckbox:(NSString *)checkboxToHighlight withHighlight:(NSString *)highlight {
if ( [checkboxToHighlight isEqualToString:#"left"] ){
[self.checkBoxLeft setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal];
} else {
[self.checkBoxRight setImage:[UIImage imageNamed:highlight] forState:UIControlStateNormal];
}
}

Resources