Owlcation »
Shank »
Computing
Produce Radiocommunication Buttons and Checkboxes for iOS Applications
Updated on February 18, 2019
Kevin Languedoc
more
else{ [radiobutton2 setSelected:YES]; [radiobutton1 setSelected:NO];
The iOS SDK and Xcode offers the rudiments in footing of UI controls. Two of the well-nigh secondhand UI controls are checkboxes and tuner buttons which are painfully absent in the UIControls that comes with the iOS SDK. Luckily, the Chocolate Feeling model offers approximately first-class enclosed APIs render the functionality needful to produce checkboxes and tuner buttons speedily.
This tutorial testament read you with piddling encrypt how to much make checkboxes and tuner buttons. Although rattling practicable to make entirely in codification, I volition be victimization predefined images of the checkboxes and tuner buttons which are highly soft to shuffle with an miscellanea of lifelike tools. In any package coating or web applications in yield, developers volition admit icons and images to assist them produce the face and tone that is mandatory. So it makes gumption to use images to mime the checkboxes and wireless buttons in an iOS package coating.
Tuner buttons and Checkboxes | Rootage
Produce Images
Earlier acquiring to the diligence which testament but want instant cryptography, I would same to shew how to manner approximately checkboxes and radiocommunication buttons. For this instance, I leave be exploitation Powerpoint, but the like burden can be achieved with a multifariousness of vivid tools which can admit Apple’s Tonic or Google’s Intro or Lottery. Thither is too Afford Berth that can be victimised or Limping to epithet a few.
The low portion of creating a checkbox is to tie two squares. This is light in Powerpoint. Add two straight shapes to a lacuna swoop. Arrange them as you want but in one of them add two lines crisscross same in the followers screenshot. Compensate penetrate on apiece icon or contour and choose “Save as Image” which bequeath permit you to preserve these images as a png charge.
Too for the radiocommunication buttons, get-go haulage a circuit, approximately .38 inches in diam. So standoff a s roofy configuration privileged the beginning qualification surely that the secondment lap is good centered inside the get-go. Initialise, the circles, are you same to flux with your app. Future, prize the beginning two and redress dog on the two images and quality “Grouping” from the circumstance fare and “Group” to grouping these two images unitedly to shape a cohesive picture. So brand a transcript of this new icon. In the arcsecond effigy, quality the camp and modification the fulfil to lightlessness or another iniquity coloration. Last, keep the two tuner buttons as ahead to the register arrangement. I birth provided a screenshot of my tuner buttons, but you can micturate yours that scoop suits your inevitably.
Anatomy the Covering
Produce a Ace Scene iOS (iPhone) lotion. Erst the task is frame-up, choice the externalize solution radical and add a new grouping by rectify clicking on this undertaking thickening and selecting new radical. Distinguish it Images. So Rightfulness clink on this new aggroup and prize “Add Files to ….” bidding and shop to the directory where you write your checkbox and tuner release images. Detent “Add” to simulate them to the projection.
ViewController Cope
In the lintel register of the ViewController custom-made stratum add iii UIButton illustration variables: checkbox, radiobutton1 and radiobutton2 as in the seed encrypt itemisation under. These bequeath be the checkbox and radiocommunication buttons in our vista posterior. Besides add two example methods: checkboxSelected and radiobuttonSelected. I testament excuse these in the effectuation lodge.
ViewController.h
//
// ViewController.h
// RadioButtonsAndCheckbox
//
// Created by Kevin Languedoc on 11/1/12.
// Copyright (c) 2012 Kevin Languedoc. All rights reserved.
//
#implication
@interface ViewController : UIViewController
@property(firm, nonatomic) UIButton *checkbox;
@property(stiff, nonatomic) UIButton *radiobutton1;
@property(substantial, nonatomic) UIButton *radiobutton2;
-(nihility)checkboxSelected:(id)transmitter;
-(nullity)radiobuttonSelected:(id)transmitter;
@end
ViewController Effectuation
viewDidAppear – Checkboxes
Beginning synthesise the variables victimisation the @synthesize directional. This is the like as creating gettter and setters. You can too ascribe a new describe to the varying if you comparable as :
@synthesize checkbox = __checkbox;
Yet for this externalise I am acting a dim-witted deduction. Succeeding I would alike to lot your aid to the viewDidAppear method in the ViewController.m inscribe itemisation downstairs, which doesn’t in the nonpayment execution but is a stock representative method in the UIViewController grade. So add it hither as in the ViewController.m cipher itemisation downstairs as antecedently cited. Therein method we are passing to initialise the checkbox UIButton exploitation the initWithFrame holding. This place takes a CGRectMake aim as stimulant. As you may acknowledge the CGRectMake aim has quatern parameters: x, y, breadth and peak. I volition set these parameters to 0, 0, 75, 75 severally. This volition post the push in the top leftfield niche of the scenery and pee-pee the push foursquare with a sizing of 75×75 pixels. Commend users pauperization to be capable to use their fingers to choice these buttons.
Future we testament put the checkbox images: CheckboxOff.png and CheckboxOn.png unless you named yours otherwise to the desktop and besides delimit the which nation the release mustiness be at in fiat to set the downplay. For the “off” land, we bequeath set the nation to UIControlStateNormaland for the “on” set the nation to UIControlStateSelected. The succeeding demarcation bequeath rig the accomplish events and what to do when the push is clicked. So add the addTarget with the @selector(checkboxSelected:) evaluate. Recall to add the “:” colon at the end of the method discover differently you bequeath get a runtime misplay. The indorsement argument is the “forControlEvents” which result leave activate the activeness. In our suit we testament use the “UIControlEventTouchUpInside” which bequeath spark when the release is released.
All that is requisite now is to add the release to the survey which we leave do with the addSubview dimension of the ViewController. Advert to the viewDidAppear method in the cipher itemisation under for a optic aid to this schoolbook.
ViewController.m – viewDidAppear For Checkboxes
-(nihility)viewDidAppear:(BOOL)alive{
//Checkboxes
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
[checkbox setBackgroundImage:[UIImage imageNamed:@»CheckboxOff.png»] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:@»CheckboxOn.png»] forState:UIControlStateSelected];
[checkbox addTarget:ego litigate:@selector(checkboxSelected:) forControlEvents:UIControlEventTouchUpInside];
Lastly lets view the radiobuttonSelected method. It uses the sender’s tag appraise with the replacement to set which wireless release is existence pushed. So it plainly sets the isSelected prop contingent which clit is pushed, toggling from YES to NO and backrest again contingent the flow valuate of the isSelected place.The nail codification is provided as incessantly and caper the included telecasting to get a flavor on how the inscribe behaves at runtime.
It checks to see if the clitoris is selected victimisation the transmitter parameter and the isSelected place. If its selected so set the prop to https://www.facebook.com/edubirdie/about/ NO differently set it to YES. This volition initiation the desktop images to shift from one to the former.
ViewController.m – checkboxSelected
-(nothingness)checkboxSelected:(id)transmitter{
if([checkbox isSelected]==YES)
{
[checkbox setSelected:NO];
}
else{
[checkbox setSelected:YES];
}
}
viewDidAppear – Tuner Buttons
The tuner buttons trace the like rule with a few exceptions. Low alternatively of one clit, thither are two but the cypher is indistinguishable omit for the CGRectMake method. The beginning radiocommunication push has the chase values: 0, 80, 75, 75. This agency that the kickoff tuner release volition situated succeeding to the remaining butt of the view but it testament be 80 pixels from the top boundary. the feather testament absorb the like blank. The s tuner clit bequeath sustain the chase CGRectMake values: 80, 80, 75, 75. This way that set future to the low tuner clit and volition engage the like place. The otc elision is that I added the tag attribute to the radiocommunication clit UIButtons. We bequeath use these in the radiobuttonSelected adjacent.
Naturally the appraise of the addTarget bequeath be dissimilar since the buttons bequeath birdsong the radiobuttonSelected method when the radiocommunication buttons are moved. Add apiece wireless release to the sight with the addSubView dimension. Let a view the provided encrypt extract on the tuner buttons to reap a amend apprehension on how to apparatus the encipher.
ViewController.m – viewDidAppear for Wireless Buttons
//radiocommunication buttons
radiobutton1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 80, 75, 75)];
[radiobutton1 setTag:0];
[radiobutton1 setBackgroundImage:[UIImage imageNamed:@»RBOff.png»] forState:UIControlStateNormal];
[radiobutton1 setBackgroundImage:[UIImage imageNamed:@»RBOn.png»] forState:UIControlStateSelected];
[radiobutton1 addTarget:ego fulfil:@selector(radiobuttonSelected:) forControlEvents:UIControlEventTouchUpInside];
radiobutton2 = [[UIButton alloc] initWithFrame:CGRectMake(80, 80, 75, 75)];
[radiobutton2 setTag:1];
[radiobutton2 setBackgroundImage:[UIImage imageNamed:@»RBOff.png»] forState:UIControlStateNormal];
[radiobutton2 setBackgroundImage:[UIImage imageNamed:@»RBOn.png»] forState:UIControlStateSelected];
[radiobutton2 addTarget:ego litigate:@selector(radiobuttonSelected:) forControlEvents:UIControlEventTouchUpInside];
[ego.survey addSubview:radiobutton1];
[ego.sight addSubview:radiobutton2];
[ego.purview addSubview:checkbox];Nevertheless if you run the app now, you bequeath CheckboxOff.png simulacrum but it won’t do anything because we silence birth to add the codification to the checkboxSelected method. The method is rather simpleton.
As you can see creating tradition radiocommunication and checkboxes is real gentle t do.
ViewController.m – radiobuttonSelected
-(vacuum)radiobuttonSelected:(id)transmitter{
substitution ([transmitter tag]) {
vitrine 0:
if([radiobutton1 isSelected]==YES)
{
[radiobutton1 setSelected:NO];
[radiobutton2 setSelected:YES];
}
else{
[radiobutton1 setSelected:YES];
[radiobutton2 setSelected:NO];
}
prisonbreak;
suit 1:
if([radiobutton2 isSelected]==YES)
{
[radiobutton2 setSelected:NO];
[radiobutton1 setSelected:YES];
}
Kevin is a Package Developer with 20 age get scheming and construction patronage word and scheme consolidation solutions.Striking WriterSeed
}
breakage;
nonpayment:
company website breakout;
}
}
ViewController.m
//
// ViewController.m
// RadioButtonsAndCheckbox
//
// Created by Kevin Languedoc on 11/1/12.
// Copyright (c) 2012 Kevin Languedoc. All rights reserved.
//
#implication «ViewController.h»
@interface ViewController ()
@end
@implementation ViewController
@synthesize checkbox, radiobutton1,radiobutton2;
– (empty)viewDidLoad
{
[superintendent viewDidLoad];
// Do any extra apparatus afterwards consignment the horizon, typically from a nib.
}
-(empty)viewDidAppear:(BOOL)alive{
//Checkboxes
checkbox = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 75, 75)];
[checkbox setBackgroundImage:[UIImage imageNamed:@»CheckboxOff.png»] forState:UIControlStateNormal];
[checkbox setBackgroundImage:[UIImage imageNamed:@»CheckboxOn.png»] forState:UIControlStateSelected];
[checkbox addTarget:ego fulfil:@selector(checkboxSelected:) forControlEvents:UIControlEventTouchUpInside];
[ego.scene addSubview:checkbox];
//radiocommunication buttons
radiobutton1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 80, 75, 75)];
[radiobutton1 setTag:0];
[radiobutton1 setBackgroundImage:[UIImage imageNamed:@»RBOff.png»] forState:UIControlStateNormal];
[radiobutton1 setBackgroundImage:[UIImage imageNamed:@»RBOn.png»] forState:UIControlStateSelected];
[radiobutton1 addTarget:ego execute:@selector(radiobuttonSelected:) forControlEvents:UIControlEventTouchUpInside];
radiobutton2 = [[UIButton alloc] initWithFrame:CGRectMake(80, 80, 75, 75)];
[radiobutton2 setTag:1];
[radiobutton2 setBackgroundImage:[UIImage imageNamed:@»RBOff.png»] forState:UIControlStateNormal];
[radiobutton2 setBackgroundImage:[UIImage imageNamed:@»RBOn.png»] forState:UIControlStateSelected];
[radiobutton2 addTarget:ego activity:@selector(radiobuttonSelected:) forControlEvents:UIControlEventTouchUpInside];
[ego.sight addSubview:radiobutton1];
[ego.horizon addSubview:radiobutton2];
}
– (nullity)viewDidUnload
{
[first-rate viewDidUnload];
// Dismissal any maintained subviews of the principal panorama.
}
– (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
reappearance (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
-(empty)checkboxSelected:(id)transmitter{
if([checkbox isSelected]==YES)
{
[checkbox setSelected:NO];
}
else{
[checkbox setSelected:YES];
}
}
-(nothingness)radiobuttonSelected:(id)transmitter{
substitution ([transmitter tag]) {
vitrine 0:
if([radiobutton1 isSelected]==YES)
{
[radiobutton1 setSelected:NO];
[radiobutton2 setSelected:YES];
}
else{
[radiobutton1 setSelected:YES];
[radiobutton2 setSelected:NO];
}
breakout;
vitrine 1:
if([radiobutton2 isSelected]==YES)
{
[radiobutton2 setSelected:NO];
[radiobutton1 setSelected:YES];
}
else{
[radiobutton2 setSelected:YES];
[radiobutton1 setSelected:NO];
}
gaolbreak;
nonpayment:
breakout;
}
}
@end
This subject is exact and genuine to the better of the author’s noesis and is not meant to backup for ball and individualised advice from a restricted master.
© 2012 Kevin Languedoc