iOS: Control two CheckBoxes - objective-c

I have this code to control two check boxes (buttons customized):
- (IBAction) setCheckBox: (id) sender{
UIImage *selected = [UIImage imageNamed:#"checkbox_checked.png"];
UIImage *notSelected = [UIImage imageNamed:#"checkbox_unchecked.png"];
if ([sender isSelected])
{
[sender setImage:notSelected forState:UIControlStateNormal];
[sender setSelected:NO];
if ([sender tag] == 10) boolOne = FALSE;
if ([sender tag] == 11) boolTwo = FALSE;
}
else
{
[sender setImage:selected forState:UIControlStateSelected];
[sender setSelected:YES];
if ([sender tag] == 10) boolOne = TRUE;
if ([sender tag] == 11) boolTwo = TRUE;
}
}
You can see that this code control two checkboxes, when I press one it become checked and when I press another time it become unchecked.
Now I want change this code in this way:
In default checkbox with tag 10 is checked and when I press it, I haven't effects, but when I press checkbox with tag 11 it should be checked and checkbox 10 should be unchecked.
The checkbox should toggle the selection and also the bool value

if([sender isSelected])
return;
if([sender tag] == 11){
[sender setSelected:YES];
[checkbox10 setSelected:NO];
}
if ([sender tag] == 10) {
[sender setSelected:YES];
[checkbox11 setSelected:NO];
}
you can get checkbox by tag value like I assume checkbox are instance of uibutton so
UIButton *checkBox10 = (UIButton*)[self.view viewWithTag:10];
I hope you understand. Also set the images according to code.

If you want the behavior of radio buttons, you should probably use radio buttons instead of check boxes.
Assuming that you store your check boxes as instance variables named checkbox10 and checkbox11, this should solve your problem:
- (IBAction) setCheckBox: (id) sender{
UIImage *selected = [UIImage imageNamed:#"checkbox_checked.png"];
UIImage *notSelected = [UIImage imageNamed:#"checkbox_unchecked.png"];
if (![sender isSelected])
{
[sender setImage:notSelected forState:UIControlStateNormal];
[sender setSelected:YES];
if ([sender tag] == 10)
{
boolOne = YES;
boolTwo = NO;
[checkbox11 setImage:notSelected forState:UIControlStateNormal];
[checkbox11 setSelected:NO];
}
if ([sender tag] == 11)
{
boolTwo = YES;
boolOne = NO;
[checkbox10 setImage:notSelected forState:UIControlStateNormal];
[checkbox10 setSelected:NO];
}
}
}

Related

UICollectionViewCell with UIButton radio button - one radioButton selected at time

I have UICollectionView with UIButton as a radio Button in UICollectionViewCell.xib.
I need one radioButton selected at time, if next was pressed, previous should be deselected. I will appreciate help.
//Loading radio button when view loads
-(void)setRadioButton{
if (!self.product.isSelectedCell) {
[self.radioBtn setBackgroundImage:[UIImage imageNamed:#"radio-button"] forState:UIControlStateNormal];
}
else
{
[self.radioBtn setBackgroundImage:[UIImage imageNamed:#"radio-button-select"] forState:UIControlStateNormal];
}
}
//Action for radio button
- (IBAction)radioBtnAction:(UIButton*)sender {
if([self.radioBtn isSelected]==YES)
{
[self.radioBtn setBackgroundImage:[UIImage imageNamed:#"radio-button-select"] forState:UIControlStateNormal];
}
else{
//always calls else
[self.radioBtn setBackgroundImage:[UIImage imageNamed:#"radio-button"] forState:UIControlStateNormal];
}
}
Use this code in cellForRowAtIndexPath
UIButton *radioButton = (UIButton *) [cell.contentView viewWithTag:kRadioButtonTag]; // Radio button
[radioButton setImage:[UIImage imageNamed:#"radio_unselected"] forState:UIControlStateNormal];
[radioButton setImage:[UIImage imageNamed:#"RadioSelected"] forState:UIControlStateSelected];
[radioButton addTarget:self action:#selector(radioButtonPressed:) forControlEvents:UIControlEventTouchDown];
if(indexPath == selectedIndexPath)
[radioButton setImage:[UIImage imageNamed:#"RadioSelected"] forState:UIControlStateSelected];
else
[radioButton setImage:[UIImage imageNamed:#"radio_unselected"] forState:UIControlStateNormal];
Radio button taped from table cell and get index path of that button
Take on class Lebel NSIndexPath variable and store selected index path.
- (void)radioButtonPressed:(UIButton *)sender {
CGPoint buttonPosition = [sender convertPoint:CGPointZero toView:tableView];
NSIndexPath *indexPath = [tableView indexPathForRowAtPoint:buttonPosition];
if (indexPath != nil) {
selectedIndexPath = indexPath
[tableView reloadData];
}
}
Hope this will help

Can't set UIButton.highlighted to NO

I have a UITableViewCell with UIButton in each cell. When the button is pressed I set its state to highlighted like in this answer:
[self performSelector:#selector(doHighlight:) withObject:[cell.subviews objectAtIndex:2] afterDelay:0.0];
Then I do this:
- (void)doHighlight:(UIButton *)sender {
if (sender.highlighted) {
[sender setHighlighted:NO];
} else {
[sender setHighlighted:YES];
}
}
But the button not only is just not highlighted at all, not speaking about the fact that I should be able to UNhighlight it.
Any ideas on what is wrong?
I ended up using UIButton's selected property. It does not require any delay and it works brilliantly with this type of things:
if (!sender.selected) {
[sender setSelected:YES];
[cell addSubview:hiddenButton];
[self performSelector:#selector(doHighlight:) withObject:sender];
} else {
[sender setSelected:NO];
[self performSelector:#selector(doUnHighlight:) withObject:sender];
}
Here is a possible solution you are looking for. I have modified my answer
UIButton *button=[cell.subviews objectAtIndex:2];
//I am adding these five lines to ensure the different
//states of the button to achieve your highlighted state goal.
UIImage *highlightImage = [UIImage imageNamed:#"highlight.png"];//Also used when button is selected
UIImage *normalImage = [UIImage imageNamed:#"normal.png"];
[button setBackgroundImage:normalImage forState:(UIControlStateHighlighted)];
[button setBackgroundImage:highlightImage forState:(UIControlStateSelected)];
[button setBackgroundImage:normalImage forState:UIControlStateNormal];
[self performSelector:#selector(doHighlight:) withObject:[cell.subviews objectAtIndex:2]];
-(IBAction) doHighlight:(id)sender
{
if ([sender isKindOfClass:[UIButton class]])
{
UIButton *btn=(UIButton*)sender;
if (btn.isSelected) {
[btn setSelected:NO];
}
else
{
[btn setSelected:YES];
}
}
}

UITextField rightView "WhileEditing" problem

I try to subclass UITextField as follows to implement custom rightView as a Clear button:
-(void) drawRect:(CGRect)rect
{
[self.layer setBackgroundColor:[[UIColor colorWithRed:20.0/255.0 green:20.0/255.0 blue:20.0/255.0 alpha:1] CGColor]];
[self.layer setCornerRadius:15.0];
UIImage *imgClear = [UIImage imageNamed:#"btnClear"];
CGSize iSize = [imgClear size];
UIButton *clearButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, iSize.width, iSize.height)];
[clearButton setImage:imgClear forState:UIControlStateNormal];
[clearButton addTarget:self action:#selector(clearText:) forControlEvents:UIControlEventTouchUpInside];
[self setRightViewMode:UITextFieldViewModeWhileEditing];
[self setRightView:clearButton];
[clearButton release];
}
but the Problem is: when the textfield just becomes focus, "clear"-button becomes visible too and after i begin to tap the keyboard it dissapears. Any ideas?
I meet the same problem, too. I guess this is an iOS bug, however, I tried to fix this problem by following implementations and it works fine for me. Hope this will help you.
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
...
[self addTarget:self action:#selector(onEditing:) forControlEvents: UIControlEventEditingChanged]
...
}
-(void) onEditing:(id)sender {
if(![self.text isEqualToString:#""]){
self.rightViewMode = UITextFieldViewModeAlways;
}else{
self.rightViewMode = UITextFieldViewModeNever;
}
}
- (BOOL)becomeFirstResponder{
BOOL ret = YES ;
ret = [super becomeFirstResponder] ;
if( ret & ![self.text isEqualToString:#""]){
self.rightViewMode = UITextFieldViewModeAlways;
}else{
self.rightViewMode = UITextFieldViewModeNever;
}
return ret ;
}
- (BOOL)resignFirstResponder
{
BOOL ret = YES ;
ret = [super resignFirstResponder] ;
if( ret )
self.rightViewMode = UITextFieldViewModeNever;
return ret ;
}
- (void) clearText:(id)sender
{
self.text = #"";
self.rightViewMode = UITextFieldViewModeNever;
}
You shoud use : [self setRightViewMode:UITextFieldViewModeAlways];
Subclass UITextField and override -layoutSubviews.
- (void)layoutSubviews
{
[super layoutSubviews];
// HACK: There is an iOS bug where the right view is not displayed when there is text in the text field. Also, iOS adds and removes the rightView. This code adds the right view and uses hide-unhide instead.
UIView *rightView = [self rightView];
if (rightView != nil && [self clearButtonMode] == UITextFieldViewModeNever) {
BOOL showRightView;
BOOL isFirstResponder = [self isFirstResponder];
switch ([self rightViewMode]) {
case UITextFieldViewModeNever:
showRightView = FALSE;
break;
case UITextFieldViewModeWhileEditing:
showRightView = isFirstResponder;
break;
case UITextFieldViewModeUnlessEditing:
showRightView = !isFirstResponder;
break;
case UITextFieldViewModeAlways:
default:
showRightView = TRUE;
break;
}
showRightView = (showRightView && ![[self text] isEqualToString:#""]);
[rightView setFrame:[self rightViewRectForBounds:[self bounds]]];
[rightView setHidden:!showRightView];
[self addSubview:rightView];
}
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
UITextField *searchField = nil;
for (UIView *subview in controller.searchBar.subviews) {
DebugLog(#"%#",[subview description]);
if ([subview isKindOfClass:[UITextField class]]) {
searchField = (UITextField *)subview;
UIImageView *clearIconView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"ClearIcon.png"]];
searchField.rightView = clearIconView;
searchField.rightViewMode = UITextFieldViewModeAlways;
[clearIconView release];
break;
}
}
}
Simple code for solve this problem
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
textField.rightViewMode=UITextFieldViewModeAlways;
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
textField.rightViewMode=UITextFieldViewModeNever;
return YES;
}
I have written an open source class, STAResizingTextField, that allows you to specify custom clear text field button images.

Hidden the button in same subview

for (id btn in [searchMenu subviews]) {
if([btn isKindOfClass:[UIButton class]]){
if ([btn tag]>=1 && [btn tag]<=3) {
if ([btn tag]==flag) {
[btn setBackgroundImage:[UIImage imageNamed:#"all_news_bg.png"] forState:UIControlStateNormal];
}else {
[btn setBackgroundImage:nil forState:UIControlStateNormal];
}
}
}
}
I have five button on searchMenu subview... when button i am clicking the btn tag 3 i need to hidden the btn tag 11.... How to do?
In the search menu subview five button tags are 0,1,2,3,11
I need to hidden the button tag 11 when i click button tag 3.
#Thanks in advance.
You must be receiving your UIButtons pressed event in an one method,
Let's assume that's buttonClicked:
-(void) buttonClicked:(id) sender
{
UIButton* myButton = (UIButton*) sender;
if(myButton.tag == 3)
{
UIButton* buttonWithTaged11 = [myButton.superview viewWithTag:11];
if(buttonWithTaged11)
buttonWithTaged11.hidden = YES;
}
}
Implement this code in the interface action method of the button with tag 3.
for( UIView *view in self.view.subviews ) {
if( [view isKindOfClass:[UIButton class]] ) {
if( view.tag == 11 )
[view removeFromSuperview];// You can hide or remove
}
}

Implementing toggle feature in a UIButton

I want to know how to add the toggling feature to a UIButton, something like the user taps a unselected button the button becomes selected and stays selected till the user taps it again there by making unselected like it was before.
I was thinking of making an IBAction which changes it from unselected to selected, how can I do that?
Heres what I tried:
-(IBAction)toggle {
//Toggle on implementation.
button.selected = YES;
button.highlighted = NO;
button.enabled = YES;
//Toggle off implementation.
if (button.highlighted == YES) {
button.selected = NO;
button.highlighted = YES;
button.enabled = NO;
}
}
Problem...
-(IBAction)toggleFav {
if (favButton == nil) {
UIImage *unselectedImage = [UIImage imageNamed:#"favUntapped.png"];
UIImage *selectedImage = [UIImage imageNamed:#"favTapped.png"];
[favButton setImage:unselectedImage forState:UIControlStateNormal];
[favButton setImage:selectedImage forState:UIControlStateSelected];
[favButton setFrame:CGRectMake(0, 0, 40, 40)];
}
if([favButton isSelected]){
//Add to menu.
[favButton setSelected:NO];
} else {
//Remove from menu.
[favButton setSelected:YES];
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
UIImage *unselectedImage = [UIImage imageNamed:#"unselected.png"];
UIImage *selectedImage = [UIImage imageNamed:#"selected.png"];
UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
[b setBackgroundImage:unselectedImage forState:UIControlStateNormal];
[b setBackgroundImage:selectedImage forState:UIControlStateSelected];
[b addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[b setFrame:CGRectMake(0, 0, 40, 40)];
[cell.contentView addSubview:b];
}
return cell;
}
-(void) buttonPressed:(UIButton *)sender
{
if([sender isSelected]){
//...
[sender setSelected:NO];
} else {
//...
[sender setSelected:YES];
}
}
Your toggleFav code doesn't make much sense.
if (favButton == nil) { checks, if favButton is present. But if you are wiring it up with IB, it should always been there at that point. And if it wasn't how could the button call this method? So do it like this:
-(void)viewDidLoad
{
//....
UIImage *unselectedImage = [UIImage imageNamed:#"favUntapped.png"];
UIImage *selectedImage = [UIImage imageNamed:#"favTapped.png"];
[favButton setImage:unselectedImage forState:UIControlStateNormal];
[favButton setImage:selectedImage forState:UIControlStateSelected];
[favButton setFrame:CGRectMake(0, 0, 40, 40)];
//....
}
-(IBAction)toggleFav:(UIButton *)sender {
if([sender isSelected]){
//...
[sender setSelected:NO];
} else {
//...
[sender setSelected:YES];
}
}
Here you'll find an example project, with a DetaiView, that holds a Button with the 2 states.
Note: I am saving the information of what button was selected in the NSUserDefaults. You should not do that. Instead you'll want to save it in the model. But as I dont have informations on your model, I am just using NSUserDefaults.
-(void)hitButton:(UIButton*)button
{
buttonOnFlag = !buttonOnFlag;
if( buttonFlag )
[self performSelector:#selector(setHighlight:) withObject:button afterDelay:0];
}
- (void)setHighlight:(UIButton*)button
{
button.highlighted = true;
}
Use button.highlighted property
You should code like this:
-(IBAction)toggle:(id)sender {
//Toggle on implementation.
if (sender.highlighted == NO)
{
sender.selected = YES;
sender.highlighted = NO;
sender.enabled = YES;
}
//Toggle off implementation.
else{
sender.selected = NO;
sender.highlighted = YES;
sender.enabled = NO;
}
}