how to check condition on check box click? - iphone

I am trying to check the condition of checkboxes.For making checkbox i use button.I changed there sizes and provide image as checked or unchecked on their click method.Now i have two checkboxes.Now i am trying to store array value on the bases of checkbox clicked.I mean that when i click check box the specific value of the array store in the string and if i uncheck then the value of string set to null.
For that i did:
-(void) checkBoxIsAcitiveClicked
{
NSLog(#"check box button clicked");
if( [UIImage imageNamed:#"checkbox_ticked.png"])
{
isActiveStr=[arrayPickerData objectAtIndex:17];
NSLog(#" is active value is %# is fetched succesfuly",isActiveStr);
[CommonVar setIsActiveStrData:isActiveStr];
NSLog(#" is active value send to cmmon class %# sent succesfuly",isActiveStr);
}
else
{
NSLog(#"no vlaue send");
isActiveStr=nil;
[CommonVar setIsActiveStrData:isActiveStr];
NSLog(#" is active value send to cmmon class %# sent succesfuly",isActiveStr);
}
}
1)CommonVar is my common class
2)setIsActiveStrData is my class method in commonvar class
3)isActiveStr is my NSMutableString.
4)arrayPickerData is my NSMutableArray
Now i dont know how to check the condition that button image is checked.png or unchecked.png
Please help me
Thank You

UIImage *uncheckmark = [UIImage imageNamed:#"checkbox_unticked.png"]; // Unticked image
[checkbutton setImage:uncheckmark forState:UIControlStateNormal];
UIImage *checkmark = [UIImage imageNamed:#"checkbox_ticked.png"]; // ticked image
[checkbutton setImage:checkmark forState:UIControlStateSelected];
[checkbutton addTarget:self action:#selector(selectedTouched:)
forControlEvents:UIControlEventTouchUpInside];
-(IBAction)selectedTouched:(id)sender{
UIButton *aButton = (UIButton*)sender;
// to check/unchecked and Implement your logic here
(aButton.selected) ? NSLog(#"Checked") : NSLog(#"Un-Checked");
[aButton setSelected:!(aButton.selected)];
}

Related

How to create Multiple buttons programmatically, get button name whenever press on particular button?

I want to create multiple buttons using MutableArray count. After creating the button, whenever pressing on particular button, I want to display that button name in a label. But Once press on button, it is always showing the MutableArray last index value name. Please help in this problem. I am fresher ti iOS. It's my code.
-(void)ViewDidLoad {
NSMutableArray *Mute_array=[[NSMutableArray alloc]initWithObjects:#"One", #"Two", #"Three", #"Four", #"Five", nil];
for (int k=0; k<[Mute_array count]; k++){
UIButton *btn_sub_category = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn_sub_category addTarget:self action:#selector(clicks:) forControlEvents:UIControlEventTouchDown];
[btn_sub_category setTitle:[Mute_Sub_Category objectAtIndex:k] forState:UIControlStateNormal];
btn_sub_category.frame = CGRectMake(278, k*130, 483, 44);
[self.view addSubview:btn_sub_category];
}
}
-(void)clicks:(id)sender{
label.text=[btn_sub_category currentTitle];
//Here a want to send the name of that clicked button current title
}
But, here button current title is always showing MutableArray last object "Five" only. I want pressed button current title.
-(void)clicks:(id)sender{
UIButton *theButton = (UIButton*)sender;
NSString *theButtonTitle = [theButton titleForState:UIControlStateNormal]
NSLog(#"theButtonTitle :- %#", theButtonTitle);
label.text=theButtonTitle;
//Here a want to send the name of that clicked button current title
}
Try this code.
I using following method to get title of button
titleForState:
Returns the title associated with the specified state.
- (NSString *)titleForState:(UIControlState)state Parameters
state
The state that uses the title. The possible values are described in UIControlState.
Return Value
The title for the specified state. If no title has been set for the
specific state, this method returns the title associated with the
UIControlStateNormal state.
Read Documentation https://developer.apple.com/library/ios/documentation/uikit/reference/UIButton_Class/UIButton/UIButton.html#//apple_ref/occ/instm/UIButton/titleForState:
Try like this:
-(void) clicks:(id)sender
{
if ([sender isKindOfClass:[UIButton class]])
{
UIButton *button = (UIButton*)sender;
NSString *title = button.currentTitle;
// do whatever you want with title
}
}
Try Like,
-(void)clicks:(id)sender{
UIButton *resultebutton= (UIButton*)sender;
label.text=resultebutton.titleLabel.text;
}

Image Checkbox in Objective-C

I'm working on a form for the iphone and I would like a female/male button choice. The buttons originally have an M and an F written on them and when you choose one, an image flips over. I managed to display the images when the buttons are pressed. The problem I was having is that they were able to both be displayed at the same time but when one is enabled the other image should disappear. I've tried to correct it and now the program crashes.
implementation file:
-(IBAction) setCheckBox2: (id) sender
{
UIImage *selected = [UIImage imageNamed:#"female.tiff"];
// UIImage *notSelected = [UIImage imageNamed:#"male.tiff"];
if (sender == selected)
{
[sender setImage:selected];
}
else
{
[sender setImage:NO];
}
Then I was going to create another function for the male image being selected. Any suggestions?
Thanks!
You are testing that sender == selected hence implying that sender is of type UIImage, and then sending setImage: to sender... with itself as parameter.
This doen't make sense. I think what you were trying to do must look something like:
-(IBAction) setCheckBox2: (UIImageView *) sender
{
UIImage *selected = [UIImage imageNamed:#"female.tiff"];
UIImage *notSelected = [UIImage imageNamed:#"male.tiff"];
if (sender.image == selected)
{
[sender setImage:notSelected];
}
else
{
[sender setImage:selected];
}
}
Why not you are using default(for not selected) and selected(for selected) property for these two state. The advantage to do this is you will not have to write a bunch of code. OK set selected image and default image.
Take a global variable of UIButton for currentSelectedButton.
connect both the UIButton with single IBActionMethod named genderButtonClicked(or you want).
Now
-(IBAction)genderButtonClicked:(UIButton)sender
{
if(currentSelectedButton)
[currentSelectedButton setSelected:FALSE];
currentSelectedButton = sender;
[currentSelectedButton setSelected:TRUE];
}
I Think this will solve your problem

Different images for highlighted UIButton state

I need 2 different images for highlighted state for an UIButton.
I've these lines of code:
- (IBAction)buttonPressed:(id)sender
{
UIImage *followImageHighlighted = [UIImage imageNamed:#"follow-hilite.png"];
UIImage *unfollowImageHighlighted = [UIImage imageNamed:#"unfollow-hilite.png"];
if ([sender isSelected]) {
// set this image for the next time the button will pressed
[sender setImage:unfollowImageHighlighted forState:UIControlStateHighlighted];
} else {
// set this image for the next time the button will pressed
[sender setImage:followImageHighlighted forState:UIControlStateHighlighted];
}
}
- (void)viewDidLoad
{
// ...
UIImage *followImage = [UIImage imageNamed:#"follow.png"];
UIImage *unfollowImage = [UIImage imageNamed:#"unfollow.png"];
[self.followButton setImage:followImage forState:UIControlStateNormal];
[self.followButton setImage:unfollowImage forState:UIControlStateSelected];
}
The problem is that every time I press the button I see the highlighted image follow-hilite.png.
Can't I change the highlighted image for a button on the road?
I think this is a bad limitation because when the button is selected (thus, "Following") and an user press it, he see the default image, then when it touch up the image is that for selected state and when the network operation is completed then the button image switch correctly to the selected one.
Ideas?
EDIT
- (IBAction)followButtonTapped:(id)sender
{
BOOL isFollowed = [sender isSelected];
NSString *urlString = isFollowed ? kUnfollowURL : kFollowURL;
// operation [...]
[self.followButton setSelected:(isFollowed) ? NO : YES];
self.user.followed = !isFollowed;
}
I explain better the problem:
button in default state: black text on white background
button in selected state: white text on black background
If the target user is not followed, the button is in default state and if I try to press it I see the correct highlighted image.
BUT if the target user is followed and the button is in selected state, if I try to press it (and hold the finger) I see the button with black text on white background. This is very ugly and this is my problem.
The IBAction is an awkward (at best, or impossible) place to configure the control. There must be some condition in your app that triggers the requirement for the different highlighted image. Configure the button when you detect that condition.
Use the "pressed" callback for taking whatever action the app is supposed to take on the press.
I've solved with:
[myButton setImage:imageSelectedHover forState:(UIControlStateSelected | UIControlStateHighlighted)];
Glad it works. You solved it by updating the application condition: self.user.followed. Now, to make it really right, try this:
- (IBAction)followButtonTapped:(id)sender
{
NSString *urlString = self.user.followed? kUnfollowURL : kFollowURL;
// operation [...]
self.user.followed = !self.user.followed;
}
The state of your model is what matters here. The selected state of the button is more like a bool that was lying around where you are keeping a copy of the real following state.
I think you need to cast sender to UIButton* before you try to modify anything important and work out your variables, because sender does not include a method or property called -isSelected. Try this instead:
- (IBAction)buttonPressed:(id)sender
{
UIImage *followImageHighlighted = [UIImage imageNamed:#"follow-hilite.png"];
UIImage *unfollowImageHighlighted = [UIImage imageNamed:#"unfollow-hilite.png"];
if ([self isSelected]) {
// set this image for the next time the button will pressed
[(UIButton*)sender setImage:unfollowImageHighlighted forState:UIControlStateHighlighted];
} else {
// set this image for the next time the button will pressed
[(UIButton*)sender setImage:followImageHighlighted forState:UIControlStateHighlighted];
}
[self isSelected] = ![self isSelected];
}

UIButton state change does not happen till after touches end

Sorry if this is a basic question, I can't find a definitive answer.
I have set up 4 buttons:
// Add the normal and selected state for each button
UIImage *buttonImage = [UIImage imageNamed:[NSString stringWithFormat:#"HotspotNumber2-%i.png",(hotspotID +1)]];
[hotspotButton setImage:buttonImage forState:UIControlStateNormal];
UIImage *buttonImageSelected = [UIImage imageNamed:[NSString stringWithFormat:#"HotspotNumber2-%is.png",(hotspotID +1)]];
[hotspotButton setImage:buttonImageSelected forState:UIControlStateSelected];
[hotspotButton setImage:buttonImageSelected forState:UIControlStateHighlighted];
[hotspotButton addTarget:self action:#selector(hotspotTouch:) forControlEvents:UIControlEventTouchDown];
And I trap the touch events in the method:
// Called when a hotspot is touched
-(void)hotspotTouch:(id)sender{
// Deselect the hotspot currently selected
if (selectedHotspot) [selectedHotspot setSelected:NO];
selectedHotspot = (UIButton *)sender;
[selectedHotspot setSelected:YES];
// Get dictionary of hot spot that is pressed
NSDictionary *hotspot = [hotspots objectAtIndex:[selectedHotspot tag]];
NSString *imageFileName = [hotspot objectForKey:ksHotspotItemKey];
if ([imageFileName length] > 0) currentImageView.image = [UIImage imageNamed:imageFileName];
}
}
The problem I have is that the highlighted image for the button does not display until the user releases their finger, which is a noticeable delay. I have seen others solve similar issues by changing the background image instead of the button state or performing a selector after a delay so the run loop gets chance to end. Both methods seem like hacks to me and would be grateful if someone could explain what is happening here and what the most robust way of achieving the effect that as soon as the user touches down on the button it changes to its highlighted state.
Thanks in advance,
Dave
Have found a work around. I have created one method for TouchDown and one for TouchUpInside and TouchUpOutside. The TouchDown simply deselects the button if it is already selected and changes my view's image. The TouchUp event sets the selected property of the button. As both the highlighted and selected images are the same the net effect is that the button changes as soon as the button is touched and remains that way after the touch up events. Code is here:
// Called when a hotspot is touched down
-(void)hotspotTouchDown:(id)sender{
// Deselect the hotspot currently selected if it exists
if (selectedHotspot) [selectedHotspot setSelected:NO];
// Get dictionary of hot spot that is pressed
NSDictionary *hotspot = [hotspots objectAtIndex:[sender tag]];
// If the class of the hotspot is 'self' then replace the current image with a new one given in the hotspot data
if ([[hotspot objectForKey:ksHotspotClassKey] isEqualToString:ksHotspotClassSelf]) {
NSString *imageFileName = [hotspot objectForKey:ksHotspotItemKey];
if ([imageFileName length] > 0) currentImageView.image = [UIImage imageNamed:imageFileName];
}
}
// Called when a hotspot is touched up
-(void)hotspotTouchUp:(id)sender{
// Set the selected property of the button
selectedHotspot = (UIButton *)sender;
[selectedHotspot setSelected:YES];
}

UIButton need clicks two times for firing its action

I have two round rect buttons. I have implemented its touch up inside action. It works fine but when my click second button just after clicking the first button, my first click has no effect. This is happening with both the buttons. I am actually trying to change background image of buttons on each click to get the effect of check box (checking/unchecking). Below is the code for for one of the buttons:
-(IBAction) checkButton2: (id) sender
{
self.checkButton2 = sender;
if ( isCheck == NO)
{
[self.checkButton2 setImage: [UIImage imageNamed:#"check.png"] forState:UIControlStateNormal];
isCheck = YES;
}
else
{
[self.checkButton2 setImage:[UIImage imageNamed:#"uncheck.png"] forState:UIControlStateNormal];
isCheck = NO;;
}
}
This is solved. Actually I was using isCheck BOOL type variable for two actions(One is given above) for two buttons and both actions were setting this variable separately which shows deviation from expected behavior.