Create checkbox - ios

Below is my implementation code:-
[self.tncCheckBoxLabel setTitle:#"Accept Terms and Conditions" forState:UIControlStateNormal];
[self.tncCheckBoxLabel setImage:[UIImage imageNamed:#"Unchecked Checkbox Filled-50.png"] forState:UIControlStateNormal];
[self.tncCheckBoxLabel setImage:[UIImage imageNamed:#"Checked Checkbox Filled-50.png"] forState:UIControlStateSelected];
//onClick method
- (IBAction)tncCheckBoxPressed:(id)sender {
//self.tncCheckBoxLabel.selected = !self.tncCheckBoxLabel.selected;
self.tncCheckBoxLabel.selected = !self.tncCheckBoxLabel.selected;
}
The initial view:
The view after clicking on the button:
The initial view is good but after clicking on the button, the image wrapped itself on the text.
I would like to display the checked image to the left which looks like the initial view.

Related

Change image on button click in ios

I have four buttons on a view controller..ViewControllerA.On click of ecah button a viewcontroller in container view opens.I want when button A is clicked then the image on button A changes and image on button B,C,D remains same .when button B is clicked then the image on button B changes and image on buttonA,C,D remains same.
when button C is clicked then the image on button C changes and image on buttonA,B,D remains same.when button D is clicked then the image on button A changes and image on buttonA,B,C. remains same
- (IBAction)AClick:(id)sender {
[_A_btn setImage:[UIImage imageNamed:#“Ahover.png"] forState:UIControlStateNormal];
[_B_btn setImage:[UIImage imageNamed:#“B.png"] forState:UIControlStateHighlighted];
[_C_btn setImage:[UIImage imageNamed:#“C.png"] forState:UIControlStateSelected];
[_D_btn setImage:[UIImage imageNamed:#“D.png"] forState:UIControlStateSelected];
__A_btn.showsTouchWhenHighlighted = YES;
}
I ma following this code .kindly suggest the changes required as it is not working as desired.
first set images for buttons for required states as well, and u can change state of the button on click as follows , it will show the image of that state.
- (IBAction)AClick:(UIButton *)sender {
sender.selected = !sender.selected;
}

UIButton selected text not showing for selected state

All,
I have a UI element that contains a button. I set the text on the button, and simultaneously, the selected state. See below:
- (void)setText:(NSString *)text {
self.inputButton.selected = text != nil; // select the buttons if the text is not nil
self.cancelButton.selected = text != nil;
[self.inputButton setTitle:text forState:UIControlStateSelected];
[self.inputButton setTitle:text forState:UIControlStateHighlighted];
}
this is working properly when filled out in the view controller.
However, when I passed the text in from the previous view controller, the selected state text doesn't display. The cancelButton selected state works properly, and the inputButton state is supposedly selected, however, selected state text isn't displaying.
I don't know if it's because the button isn't redrawing or something? I've tried all the [self layoutIfNeed] and [self setNeedsDisplay] with no luck. Anyone know what's going on?
I figured out the issue. I was using both the selected and highlighted states. For some funky reason, that was creating a situation where the button state could be 5. I resolved the problem by not addressing the UIControlStateHighlighted in any way.
Can you check adding following line of code.
- (void)setText:(NSString *)text {
self.inputButton.selected = text != nil; // select the buttons if the text is not nil
self.cancelButton.selected = text != nil;
[self.inputButton setTitle:text forState:UIControlStateSelected];
//Start:Added line of Code
[self.inputButton setTitle:text forState:UIControlStateNormal];
//End:
[self.inputButton setTitle:text forState:UIControlStateHighlighted];
}
Thanks & Regards,
Amit

Changing the Second Button Image by Clicking the First Button IOS7

Hi I'm trying to change the button image by clicking the another button for example i have two buttons like button1 and button2 already this two button has images but now I'm trying to change the second button image by clicking the first button in second button the new image should replace the old image please tell me how to do it.
- (IBAction)bt1:(id)sender {
UIButton *btn = (UIButton*)sender;
[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
}
- (IBAction)bt2:(id)sender {
}
**[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"]** forState:UIControlStateNormal];
This above code is used to change the button image but i want know how to change the second button image by clicking image.
Thanks
You might have the IBAction property of a second button, which you can access in your implementation (.m) file with self (e.g. self.secondButtonPropertyName), now you can write the code like below:
-(IBAction)bt1:(id)sender
{
[self.btn2 setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
}
first you set your both button tag..like btn1 tag =1 and btn2 tag =2
- (IBAction)bt1:(id)sender {
UIButton *btn = (UIButton*)[self.view viewWithTag:2];
[btn setBackgroundImage:[UIImage imageNamed:#"vv.png"] forState:UIControlStateNormal];
}
- (IBAction)bt2:(id)sender {
}
suppose you have two buttons btn1 and btn2
Now after them allocatng memory,
you can use below Button selector,
btn1 selector,
-(IBAction)btn1selector:(id)sender
{
[btn2 setbackgroundimage: [uiimage imagenamed:#"your image"]];
}
you can do same for the other button selector.

Changing a UIButton's image when it's tapped

I have a UIButton that I am programmatically creating and adding to a UITableViewCell.
I have successfully set it up so that if you tap the button and keep holding it down, it will change to the image that I have set for the "highlighted state", but this is not good enough.
When the user taps the button, I need it to completely change to the new image. If the user taps the button again, I want it to change back to the original image.
I want the image to change every time they tap. Right now it changes if they tap and keep holding it down, but it switches back to the original image as soon as they are done holding it down.
Here is the code that I have so far for my buttons:
UIImage *addFriendButtonImage = [UIImage imageNamed:#"SliderThumb-Normal-G"];
UIImage *addFriendButtonImageHighlighted = [UIImage imageNamed:#"SliderThumb-Normal"];
UIButton *addFriendButton = [[UIButton alloc]init];
addFriendButton.frame = CGRectMake(237, -10, 64, 64);
[addFriendButton setImage:addFriendButtonImage forState:UIControlStateNormal];
[addFriendButton setImage:addFriendButtonImageHighlighted forState:UIControlStateHighlighted];
I also tried setting the state for the new image to "UIControlStateSelected" but all that does it make the original image a little darker. It doesn't even change to the new image, and once again it only shows it's effect if you are holding the button down.
Set the image for both the UIControlStateHighlighted and UIControlStateSelected states:
[addFriendButton setImage:addFriendButtonImage forState:UIControlStateNormal];
[addFriendButton setImage:addFriendButtonImageHighlighted forState:UIControlStateHighlighted];
[addFriendButton setImage:addFriendButtonImageHighlighted forState:UIControlStateSelected];
Then listen for UIControlEventTouchUpInside:
[addFriendButton addTarget:self action:#selector(handleTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
Then update the selected state:
- (void)handleTouchUpInside:(UIButton *)sender {
sender.selected = !sender.selected;
}
What you want is to set new image with [addFriendButton setImage:someImage forState:UIControlStateNormal]; every time the user taps on addFriendButton (look for event UIControlEventTouchUpInside).
If you want you can still assign appropriate highlight image on [addFriendButton setImage:someImage_highlighted forState:UIControlStateHighlighted];
You need to create an IBAction method for your button. If you are using a storyboard to design your view controllers go to the respective view controller in the assistant editor (tap on the icon in the upper right corner of the Xcode window), CTRL+drag the a line from the button to the .h file of your view controller's class like this:
Select Action and enter a named for the method like touchUpInsideButton.
Repeat the same steps to create a property for your button. (But select Outlet instead of Action and give your button a name like myButton.) Now you should have these two lines in your .h file:
#property (strong, nonatomic) IBOutlet UIButton *myButton;
- (IBAction)touchUpInsideButton:(id)sender;
Now go to the .m file of your view controller. Search for the newly created action method - (IBAction)touchUpInsideButton:(id)sender and enter the following code to change the button's image when the user taps the button:
- (IBAction)touchUpInsideButton:(id)sender {
UIImage *myImage = [UIImage imageNamed:#"myImageName"];
[self.myButton setImage:myImage forState:UIControlStateNormal];
}
(You can put any code inside this method that is executed whenever the user taps the button.)
I would recommend this method if you are using a storyboard. If not please refer to #Corey's reply.

Changing Image of Button When the button is selected In Iphone

I have two buttons on my first view,so when I click on one of the button the view changes,so I have two images one for the default state and one for the selected state,
first i tried with xib,goin into the properties and changing the states and then selecting the proper images and when I build and run my code,On cliking the image doesnt change..
So I did this way through code
- (IBAction) handleButton:(id)sender
{
UIButton *button = (UIButton*)sender;
int tag = [button tag];
switch (tag)
{
case BUTTON_1:
if ([m_Button1 isSelected])
{
[m_Button2 setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[m_Button1 setSelected:NO];
}
else
{
[m_Button1 setImage:[UIImage imageNamed:#"image_pressed.png"] forState:UIControlStateSelected];
[m_Button1 setSelected:YES];
}
[self displaymethod1];
break;
case BUTTON_2:
[self displaymethod2];
break;
default:
break;
}
}
here the image changes when i click on it and i go to diffrent view..when i again come back to my first view,the button is still in selected mode..so How shall i fix this..
Waiting for your reply
I think is a bit simpler through IB.
When you add a regular Round Rect Button in IB you can modify its behavior by going to the Button section in the Attributes Inspector panel.
First select the images you want for it's default state by leaving the State Config in Default. There is an image and a background image properties for this. Once that is set, you can change the State Config to Highlighted and select the image you want to show when the button is highlighted.
NOTE: This is for xcode4.
when your view will appear then you have to initiate all the variable and UI property
in view will appear method. (not necessary in all cases solution is only for your requirement)
There you can set your button default state and image.
-(void)viewWillAppear:(BOOL)animated
{
[m_Button2 setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[m_Button1 setSelected:NO];
...
}

Resources