Create Checkbox using Storyboard in Xcode - ios

I am new to Objective-C so i am mostly using Storyboard, Someone Please tell me how to create a checkbox using Xcode in iOS.

UISwitch is the standard control used in iOS for indicating an on/off, selected/unselected state. Why not use that?

UISwitch is the standard control used in IOS applications for making binary choices but if you want to use checkbox you can create a UIButton
#property (weak, nonatomic) IBOutlet UIButton *CheckBox;
- (IBAction)CheckBoxClick:(id)sender
and change its background image on click event of UIButton
- (IBAction)CheckBoxClick:(id)sender {
if(!checked){
[_CheckBox setImage:[UIImage imageNamed:#"checked.png"] forState:UIControlStateNormal];
checked = YES;
}
else if(checked){
[_CheckBox setImage:[UIImage imageNamed:#"unchecked.png"] forState:UIControlStateNormal];
checked = NO;
}
}
also there are more detailed answers to this question at
How to create a simple checkbox in iOS?
and if you dont want to use images you can check the following
Checkbox in iOS application

CheckBox is not available in object library. You can use third party library for that purpose or you can create it by your self.
There is the working code for checkbox.
create a class level variable and property of button in #inteface
#interface testViewController (){
BOOL checkBoxSelected;
}
#property (weak, nonatomic) IBOutlet UIButton *checkBox;
#end
in viewdidload set images for the button states.
[_checkBox setBackgroundImage:[UIImage imageNamed:#"checkBoxUnChecked.png"]
forState:UIControlStateNormal];
[_checkBox setBackgroundImage:[UIImage imageNamed:#"checkBoxChecked.png"]
forState:UIControlStateSelected];
and after create a button action in that button Action.
checkBoxSelected = !checkBoxSelected; /* Toggle */
[_checkBox setSelected:checkBoxSelected];
Hope it helps

1) Create Prototype cell in UITableView.
2) Add one button inside the cell and set button style to Custom.
3) Set the image for checkbox.

ios language doesn't use checkbox controller.
but you are use checkbox that you are inport two image select & unselect
Step 1> Create button and set image.
Step 2> Create button touch object method and put if condition for check & uncheck.
for example:
- (IBAction)btnLogin:(id)sender {
UIButton *btn = (UIButton *)sender;
if (btn.tag == 0) {
btn.tag = 1;
//set image checked.
}
else{
btn.tag = 0;
//set image unchecked.
}
}

You shouldn't need to subclass the UIButton class. By design, Objective-C favors composition over inheritance.
UIButton is a subclass of UIControl, which has a selected property. You can use this property to toggle the on/off behaviour of a checkbox, just the same way a UISwitch does.
You can attach an action to the button's touched up inside event, and perform the toggling in there, something like this:
// when you setup your button, set an image for the selected and normal states
[myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateSelected];
[myCheckBoxButton setImage:nonCheckedImage forState:UIControlStateNormal];
- (void)myCheckboxToggle:(id)sender
{
myCheckboxButton.selected = !myCheckboxButton.selected; // toggle the selected property, just a simple BOOL
}

Related

when i click a button in ios app i want the image to change

the title much explains it really in my button
- (IBAction)LikeBtn:(id)sender
when the button is clicked i want ti to swap images from a small grey icon to a green icon
im trying it this way but i cant seem to get it to work
- (IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage :#"liked.png" forState:UIControlStateSelected];
{
Has Any body any ideas how this can be done at the moment the image is selected in the xcode control panel and is named likebtn.png and need it changing to liked.png after clicked
Assuming you have the image properly added to your project, and it's a png:
- (IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:#"liked"] forState:UIControlStateSelected];
}
PS: Are you sure you wish to set the image for the selected state? If not, use UIControlStateNormal instead.
I think you need to setImage in viewDidLoad when creating button:
[LikeBtn setImage :[UIImage imageNamed:#"liked.png"] forState:UIControlStateSelected];
In addition to what Maddy says about passing a UIImage rather than a string for a selected state, you will also need to declare that the button is now selected. If you change your code to this, it should work for you
-(IBAction)LikeBtn:(id)sender {
UIButton *senderButton = (UIButton *)sender;
[senderButton setImage:[UIImage imageNamed:#"liked.png"] forState:UIControlStateSelected];
senderButton.selected = YES;
}
You can also put a if (senderButton.selected) condition within the IBAction method to detect and toggle between button states, changing the state from YES to NO.
Hope this helps

How to set Background image of button

I have a question and I didn't find an answer on it yet. I would like to know how to set the background image of a button in your code. I would like to use a value to change the background.
For example: if (value == one) { set background of button code}
I have two backgrounds but when you give a code you can use buttonimage1.png, buttonimage2.png.
Thnx
First you should add IBOutlet for your button, example:
#property(nonatomic, weak) IBOutlet UIButton *bttn;
next, using this outlet you can change background:
if (value == one) {
[self.bttn setBackgroundImage:[UIImage imageNamed:#"buttonimage1.png"]
forState:UIControlStateNormal];
} else {
[self.bttn setBackgroundImage:[UIImage imageNamed:#"buttonimage2.png"]
forState:UIControlStateNormal];
}
that's all

how would is change background image using UIsegmented control

how would is change background image using UIsegmented control ?
i want to change the background image using segment control and after select that image for above process.
Here's a link to the documentation for UISegmentedControl.
Edit
Maybe I had misunderstood your question. If you want to change the background image of a view, you can add your controller as a target for the segmented control.
[segmentedControl addTarget:self action:#selector(updateBackgroundImage:) forControlEvents:UIControlEventValueChanged];
When the UIControlEventValueChanged gets triggered for your control you can use any of this methods to change the background image you want.
- (void)updateBackgroundImage:(UISegmentedControl *)sender {
self.imageView = [UIImage imageNamed:(sender.selectedSegmentIndex ? #"foo": #"bar")];
}
Original Answer
As you'll find it that document you can change the appearance of the control in different ways. A few examples.
-[UISegmentedControl setBackgroundImage:forState:barMetrics:]
-[UISegmentedControl dividerImageForLeftSegmentState:rightSegmentState:barMetrics:]
-[UISegmentedControl setImage:forSegmentAtIndex:]
When the UIControlEventValueChanged gets triggered for your control you can use any of this methods to change the sender's appearance.
You have to add Target to segmented controller like
Then, when you click on any segment your "action" method will get called , change background image or do anything in "action" method
[segmentedControl addTarget:self action:#selector(action:)forControlEvents:UIControlEventValueChanged];
Method implementation should like
-(void)action:(id)sender
{
//code for setting background image.
}
Set the IBAction to your segmentbar with UIControlEventValueChanged event type and based on selected index you can change the image of your imageView
-(IBAction)segmentedChartButtonChanged:(id)sender
{
switch (self.segmentedButton.selectedSegmentIndex) {
case 0:
self.imageView.image = [UIImage imagenamed:#"imageName"];
break;
case 1:
self.imageView.image = [UIImage imagenamed:#"imageName"];
break;
default:
break;
}
}
Try this one.. May be it will help you..
First take UIImageView and set IBOutlet of that and give name like Backgroundimg And also add Segment value change method And also set outlet to Imageview in xib and outlet of method to UISegmentControl.
//.h file
IBOutlet UIImageView *Backgroundimg;
-(IBAction)SegmentChanged:(id)sender;
And just add below code in .m file
//.m file
-(IBAction)SegmentChanged:(id)sender
{
UISegmentedControl *seg=(UISegmentedControl *)sender;
if(seg.selectedSegmentIndex==0)
{
[Backgroundimg setImage:[UIImage imageNamed:#"intro1.png"]];
}
else if(seg.selectedSegmentIndex==1)
{
[Backgroundimg setImage:[UIImage imageNamed:#"intro2.png"]];
}
}

XCode Create a Button Toggle that shows one image for On and one for Off with function

I am trying to create a toggle button in XCode that is an On and Off switch for toggling incoming text messages. When the feature is on the image is designed to say ON. I need to set this image property for the state that the button is on. When the image is off. The button has an image that says Off.
What is the best route to complete this code.
Ryan
I use:
//Create a Switch
UISwitch *myswitchbutton;
myswitchbutton.frame=CGRectMake(10, 10, 40, 50);
[self addsubview:myswitchbutton];
Then, add images
myswitchbutton.offImage=[UIImage imageNamed:#"off.png"];
myswitchbutton.onImage=[UIImage imageNamed:#"on.png"];
Where off.png and on.png are images with your custom design
You can add UISwitch for this :
So there can be two ways for this :
(1). Create a UISwitch object and then add it like this :
UISwitch *onoff = [[UISwitch alloc] initWithFrame: CGRectMake(x_value,y_value,79,27)];
[onoff addTarget: self action: #selector(flip:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview: onoff];
// This is the method called when switch state changes
- (IBAction) flip: (id) sender
{
UISwitch *onoff = (UISwitch *) sender;
if(onoff.on)
{
// switch is on
}
else
{
// switch is off
}
}
(2). Drag UISwitch using interface builder onto your view and connects its outlet and add target for switch events.
The on/off images for a UISwitch can be customized using the onImage and offImage properties as described here.
There are many sample codes which can be used for custom switch :
1.simpleswitch
2. rcswitch
3.DCRoundSwitch
Hope it helps you.

UIButton image only changes for one button

I have written some code which toggles 2 buttons depending on which one is selected. If the UK one is selected it becomes ticked and the BR one becomes unticked, and vice versa. However, this only seems to be the case for the UK button. If I select the BR button than the UK button unticks, the BR button briefly ticks but then it unticks again.
I have linked up my buttons correctly (I have triple checked), and as the BR button briefly ticks it is definitely linked up. The code I am using is below:
.h
#property (weak) IBOutlet UIButton *btUK;
#property (weak) IBOutlet UIButton *btBR;
.m
- (IBAction)changePortal:(id)sender
{
UIButton *button = (UIButton *)sender;
if (button.tag == kUKButton)
{
self.btUK.imageView.image = [UIImage imageNamed:#"tick_box.png"];
self.btBR.imageView.image = [UIImage imageNamed:#"tick_box_empty.png"];
[Singleton sharedSingleton].bUseUKPortal = YES;
}
else if (button.tag == kBRButton)
{
self.btBR.imageView.image = [UIImage imageNamed:#"tick_box.png"];
self.btUK.imageView.image = [UIImage imageNamed:#"tick_box_empty.png"];
[Singleton sharedSingleton].bUseUKPortal = NO;
}
}
I have set break points within the code and have confirmed that both the buttons go in to their relevant sections when clicked. I can also confirm that no other code is using the btUK and btBR variables as I have just written it all.
Both buttons have changePortal set as their action, and the function is only called once per click.
I have also tried cleaning the code but this did not fix my issue.
If anyone can shed any light as to why this is happening then I would be very grateful.
The correct way to set the image of a UIButton is to call setImage:forState:.
So try to alter your code to something like this:
// Follow this pattern for every button image change
[self.btUK setImage:[UIImage imageNamed:#"tick_box.png"] forState:UIControlStateNormal];
Now regarding the imageView property the documentation states that:
The button’s image view. (read-only)
#property(nonatomic, readonly, retain) UIImageView *imageView
Discussion
Although this property is read-only, its own properties are
read/write. Use these properties to configure the appearance and
behavior of the button’s view. For example:
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
button.imageView.exclusiveTouch = YES;
The imageView property returns a value even if the button has not been
displayed yet. The value of the property is nil for system buttons.

Resources