IPhone UI Button ,custom checkbox tutorials

IPhone UI Button ,custom checkbox tutorials
Checkbox is one of the elements that is frequently required in iPhone UIs, but the traditional checkbox is not present in iPhone sdk. Switches are sometimes used in the place of checkboxes.
In this tutorial, we will see how to create a custom checkbox. It extends from UIButton class. It is used to select or deselect a particular item or more than one items in a list. For example: The “Keep me signed in” checkbox in certain apps, sound on/off , effects on/off checkboxes in Settings of a gaming app.If more checkboxes are needed, we can create them similarly.
Step 1: Create a window based application in Xcode and name it “MICheckBox”.
Step 2: Create new “MICheckBox.h” and “MICheckBox.m” files which extend from UIView.
(Classes >> Add >> New File >> Objective C Class. Select UIView in the “subclass of” list.)
Step 3: Create a new group in the Classes folder and name it “MICheckBoxUtils”. Drag the “MICheckBox.h” and “MICheckBox.m” files into the group.Now, add the images “checkbox_not_ticked.png” and “checkbox_ticked.png” to the group.
Step 4: Open the “MICheckBox.h” file and make the following changes in it.

1@interface MICheckBox : UIButton {
2   BOOL isChecked;
3}
4@property (nonatomic,assign) BOOL isChecked;
5-(IBAction) checkBoxClicked;

Here, we have changed the UIView class from which it extends to UIButton. Also, we have declared a boolean property “isChecked” which
keeps a track of whether the button is checked or not. “checkBoxClicked” is the method to change the state of checkbox when it is clicked.
Step 5: Now, open the “MICheckBox.m” file and add the following code to it.
01#import "MICheckBox.h"
02
03@implementation MICheckBox
04@synthesize isChecked;
05
06- (id)initWithFrame:(CGRect)frame {
07     if (self = [super initWithFrame:frame]) {
08     // Initialization code
09
       self.contentHorizontalAlignment =
11     UIControlContentHorizontalAlignmentLeft;
12
13    [self setImage:[UIImage imageNamed:
14@"checkbox_not_ticked.png"]
15     forState:UIControlStateNormal];
16
17 [self addTarget:self action:
18@selector(checkBoxClicked)
19forControlEvents:UIControlEventTouchUpInside];
20}
21    return self;
22}
23
24-(IBAction) checkBoxClicked{
25      if(self.isChecked ==NO){
26            self.isChecked =YES;
27              [self setImage:[UIImage imageNamed:
28 @"checkbox_ticked.png"]
29 forState:UIControlStateNormal];
30
1  }else{
32 self.isChecked =NO;
33 [self setImage:[UIImage imageNamed:
34@"checkbox_not_ticked.png"]
35forState:UIControlStateNormal];
36
37 }
38}
39
40- (void)dealloc {
41 [super dealloc];
42}
43
44@end

initWithFrame” method – This method initializes the button with the frame given by us. The contentHorizontalAlignment property allows us to set the alignment of the content (i.e image and text) of the button(checkbox). Initially, the image of the button will be the unselected checkbox, so we set the image “checkbox_not_ticked.png”. Then we programmatically link the button to the “checkBoxClicked” method.
“checkBoxClicked” method – In this method, whenever the checkbox is clicked, the image is changed from checked to unchecked and vice versa.
Step 6: Now that we have created the “MICheckBox” files, we need to test them. Hence, put the following code in the “MICheckBoxAppDelegate.m” file.
01#import "MICheckBoxAppDelegate.h"
02#import "MICheckBox.h"
03
04@implementation MICheckBoxAppDelegate
05@synthesize window;
06
07- (void)applicationDidFinishLaunching:(UIApplication *)application {
08
09       // Override point for customization after application launch
10       MICheckBox *checkBox =[[MICheckBox alloc]
11       initWithFrame:CGRectMake(100, 200, 150, 25)];
12
13       [checkBox setTitleColor:[UIColor blackColor]
14       forState:UIControlStateNormal];
15
16       [checkBox setTitle:@"Checkbox"
17        forState:UIControlStateNormal];
18
19       [window addSubview:checkBox];
20      [window makeKeyAndVisible];
21}
22
23- (void)dealloc {
24                 [window release];
25                 [super dealloc];
26}
27
28@end

Here, we have imported the “MICheckBox.h” file so that we can create its instance. In the “applicationDidFinishLaunching” method, we create an object of MICheckbox and set its title color to black. Also, we set its title to CheckBox. Then we add the checkbox to the main window by the “addSubview” method.

YOU CAN DOWLOAD THE TUTORIAL HERE ON CINTERVIEWS.COM

Output is like This:

  

No comments: