I have a custom uitableviewcell in that i have an imageview with a button when user clicks the button, button should show with a tick mark!
When user clicks the button it should show with a tickmark
I have a tried some thing. pls help me
checkBoxButton.selected=!checkBoxButton.selected;
if(checkBoxButton.selected==1){
[checkBoxButton setBackgroundImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
}
else{
[checkBoxButton setBackgroundImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
}
You can add properties which represent the status.
#property (nonatomic, assign) BOOL checked;
- (void)setChecked:(BOOL)checked
{
_checked = checked;
if (_checked) {
[self.checkboxButton setImage:[UIImage imageNamed:#"btn-checkbox-checked"] forState:UIControlStateNormal];
} else {
[self.checkboxButton setImage:[UIImage imageNamed:#"btn-checkbox-unchecked"] forState:UIControlStateNormal];
}
}
You may add target-action to your checkBoxButton. In the action method, set the value of checked property.
Try following code in your button click. This will work only if you have single
checkbox..
if ([self.checkBoxButton.currentTitle isEqualToString:#"uncheck"]) {
NSLog(#"changing to checked");
[self.checkBoxButton setTitle:#"check" forState:UIControlStateNormal];
[self.checkBoxButton setImage:[UIImage imageNamed:#"checkedbox.png"] forState:UIControlStateNormal];
}else if([self.checkBoxButton.currentTitle isEqualToString:#"check"])
{
NSLog(#"changing to UNchecked.....");
[self.checkBoxButton setTitle:#"uncheck" forState:UIControlStateNormal];
[self.checkBoxButton setImage:[UIImage imageNamed:#"uncheckedbox.png"] forState:UIControlStateNormal];
}
Hope this helps you...
Related
When I select the check box the second time it shows me the checked image, but the status is unchecked.
Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
[CheckBox setBackgroundImage:[UIImage imageNamed:#"uncheck_checkbox.png"] forState:UIControlStateNormal];
}
- (IBAction)CheckBox:(id)sender {
if (CheckBox.selected == NO)
{
CheckBox.selected = YES;
[CheckBox setBackgroundImage:[UIImage imageNamed:#"checked_checkbox.png"] forState:UIControlStateNormal];
}
else
{
CheckBox.selected = NO;
[CheckBox setBackgroundImage:[UIImage imageNamed:#"uncheck_checkbox.png"] forState:UIControlStateSelected];
}
}
Your code is totally right just you replace UIControlStateSelected to UIControlStateNormal.
You need add normal image and selected images in IB itself (or in viewDidLoad) and toggle state for button in button action like below
- (IBAction)checkBoxAction:(UIButton *)sender {
sender.selected = !sender.selected;
}
if (sender.tag==0)
{
sender.tag = 1;
[sender setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
}
else
{
sender.tag=0;
[sender setImage:[UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
}
When create button for check box you can save address this element on array after click on every button first access to all object on array seved and set image unckeck and this button clicked set image checked.
Or
When create button for check box set tag different for every button and when click on button you should be set image unckeck for every button use tag number and set image check for button clicked.
Also one thing,
Here you use CheckBox.selected = YES/NO;
So also you can write your code like:
- (void)viewDidLoad {
[super viewDidLoad];
CheckBox.selected = NO;
[CheckBox setBackgroundImage:[UIImage imageNamed:#"uncheck_checkbox.png"] forState:UIControlStateNormal];
[CheckBox setBackgroundImage:[UIImage imageNamed:#"checked_checkbox.png"] forState:UIControlStateSelected];
}
- (IBAction)CheckBox:(id)sender {
if (CheckBox.selected == NO)
{
CheckBox.selected = YES;
}
else
{
CheckBox.selected = NO;
}
}
- (IBAction)changeState:(id)sender {
UIButton *btn=(UIButton *)sender;
if (btn.selected == NO)
{
btn.selected = YES;
[btn setBackgroundImage:[UIImage imageNamed:#"checked_checkbox.png"] forState:UIControlStateNormal];
}
else
{
btn.selected = NO;
[btn setBackgroundImage:[UIImage imageNamed:#"uncheck_checkbox.png"] forState:UIControlStateNormal];
}
}
You should set boolean value to false in viewDidLoad and then in button click
- (IBAction)btnCheckBoxClick:(id)sender
{
if (isChecked)
{
[btnCheckBox setImage:[UIImage imageNamed:#"btncheckbox.png"] forState:UIControlStateNormal];
isChecked = false;
}
else
{
[btnCheckBox setImage:[UIImage imageNamed:#"btncheckbox_selected.png"] forState:UIControlStateNormal];
isChecked = true;
}
}
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
{
I have taken two custom buttons and one method, to set their check and uncheck
In viewdidload() i have written like this..
[checkbox_01 setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[checkbox_02 setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
isChecked_01 = NO; //declared as boolean to change check and uncheck for button one
isChecked_02 = NO; //declared as boolean to change check and uncheck for button two
And in method
-(IBAction)checkboxOnClick:(UIButton *)sender {
if (sender.tag == 1) {
if (isChecked_01 == NO) {
[checkbox_01 setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
isChecked_01 = YES;
}
else
{
[checkbox_01 setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
isChecked_01 = NO;
}
}
if (sender.tag == 2) {
if (isChecked_02 == NO) {
[checkbox_02 setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
isChecked_02 = YES;
}
else
{
[checkbox_02 setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
isChecked_02 = NO;
}
}
}
is there any other way to create check boxes in ios..? i have taken each checkbox with one isChecked boolean value for it.. is there posibilities to use only one boolean for entire check list.....
You can use selected property (inherited from UIControl) instead of your isChecked_01 and isChecked_02:
[checkbox_01 setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[checkbox_02 setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
[checkbox_01 setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
[checkbox_02 setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateSelected];
- (IBAction)checkboxOnClick:(UIButton *)sender {
sender.selected = !sender.selected;
}
Check this sample code, might be useful to if you want to do customization with checkboxes
https://github.com/Marxon13/M13Checkbox
In Storyboard
Set default checkbox image as like below.
Change the State Config to Selected and add image like checkbox enabled
In CODE
Objective C
- (IBAction)btnCheckTerms:(UIButton *)sender {
_btnCheckboxTerms.selected = !_btnCheckboxTerms.selected; }
SWIFT
#IBAction func btnCheckTerms(sender : UIButton) {
_btnCheckboxTerms.isSelected = !_btnCheckboxTerms.isSelected; }
Extra Tips
Use material Icons rather than normal icon images.
In UITableView cellForRowAtIndexPath create one button with one image. write action for that button click to change the button images. here the code.
ButtonImage = [UIImage imageNamed:#"work_exe_old.png"];
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(250,10,34,34);
myButton.tag=22;
[myButton setBackgroundImage:ButtonImage forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(select_id:)forControlEvents:UIControlEventTouchDown];
- (IBAction)select_id:(id)search
{
UIButton *tempBtn =(UIButton *) search;
[tempBtn setImage:[UIImage imageNamed:#"work_exe.png"] forState:UIControlStateNormal];
}
clicked the button images changed(work_exe.png). wen click the same button again means the image want to change (work_exe_old.png) its like check box in tableview. how can i achieve tis
Instead of doing it in this way you can customise your code much better. Check the code below:
ButtonImage = [UIImage imageNamed:#"work_exe.png"];
ButtonImageSelected = [UIImage imageNamed:#"work_exe_old.png"];
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
myButton.frame = CGRectMake(250,10,34,34);
myButton.tag=22;
[myButton setBackgroundImage:ButtonImage forState:UIControlStateNormal];
[myButton setBackgroundImage:ButtonImageSelected forState:UIControlStateSelected];
[myButton addTarget:self action:#selector(select_id:)forControlEvents:UIControlEventTouchDown];
And you can implement this selector method like this:
-(IBAction)select_id:(UIButton *) tempBtn {
[tempBtn setSelected:! tempBtn.selected];
}
set the uibutton's background images for each of its control states,
e.g.
UIImage *normal = [UIImage imageNamed:#"work_exe.png"];
UIImage *highlighted = [UIImage imageNamed:#"work_exe_old.png"];
UIImage *selected = [UIImage imageNamed:#"work_exe_old.png"];
[myButton setBackgroundImage:normal forState:UIControlStateNormal];
[myButton setBackgroundImage:highlighted forState:UIControlStateHighlighted];
[myButton setBackgroundImage:selected forState:UIControlStateSelected];
If you want do a action by clicking on UITableViewCell you have to implement the didSelectRowAtIndexPath and before decalre int i; gobally then follow the below code to make the changes for Button image
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
switch (i) {
case 0:
if(indexPath.row==0)
[tempBtn setImage:[UIImage imageNamed:#"work_exe.png"] forState:UIControlStateNormal];
}
else if (indexPath.row==1)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exe.png"] forState:UIControlStateNormal];
}
else if (indexPath.row==2)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exe.png"] forState:UIControlStateNormal];
}
.
.
.
else if (indexPath.row==n)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exeN.png"] forState:UIControlStateNormal];
}
i=1;
break;
case 1:
if(indexPath.row==0)
[tempBtn setImage:[UIImage imageNamed:#"work_exe0.png"] forState:UIControlStateNormal];
}
else if (indexPath.row==1)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exe1.png"] forState:UIControlStateNormal];
}
else if (indexPath.row==2)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exe2.png"] forState:UIControlStateNormal];
}
.
.
.
else if (indexPath.row==n)
{
[tempBtn setImage:[UIImage imageNamed:#"work_exeN.png"] forState:UIControlStateNormal];
}
i=0;
break;
default:
break;
}
[yourtablename reloadData];
note: write this code after the cellForRowAtIndexPath method
I am not sure weather I understand you right. If you want the image to change after a click, you should save the current state of your image somewhere, e.g. in an NSInteger that holds the state for your cell as a number that represents which image should be shown. In the tableView:cellForRowAtIndexPath: method, put the right image (according to your NSInteger) into the cell.
#interface MyTabelViewClass()
#property (assign) NSInteger currentImageState;
...
#end
In the tableView:cellForRowAtIndexPath: method, you somewhere have to decide which image to chose, according to this property:
switch (self.currentImageState) {
case 0:myCell.imageView = [[UIImageView imageViewWithImage:[UIImage imageNamed:#"..."]]; break;
case 1:myCell.imageView = [[UIImageView imageViewWithImage:[UIImage imageNamed:#"..."]]; break;
...
}
In the tableView:didSelectRowAtIndexPath you have to change the state somewhere, like
self.currentImageState = (self.currentImageState + 1) % maximumNumberOfStates;
...
[self.myTableView reloadData]
...
Maybe it's a good idea to call reloadData with some delay because you want to wait for the deselection animation to finish first. You can do this by calling
[self.myTableView performSelector:#selector(reloadData) withObject:nil afterDelay:0.35]
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];
}
}