Tuesday, 29 March 2011

In this post I will demonstrate on how to use a slider control.

Overview: In this post we will change the value of text as per the value of the slider,

Step 1: Open Xcode and create a window based or a view based project and add two UIViewController subclass give an appropriate name to the file and select the .h file of the viewController subclass and add this piece of code. The name of my viewcontroller class is sliderviewController.

#import


@interface sliderviewController : UIViewController {

UILabel *mylabel;
UISlider *slider;
}
-(void)changeLabelText;

@end

in the above code you can see that I have made the objects of the label and slider also I have made a function called changeLabelText which will be called by the slider

Step 2: In this step we will go to the .m file of sliderviewController and perform some settings like setting the frame and text for the label and slider control into the init method here's how we do that

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
mylabel = [[UILabel alloc]initWithFrame:CGRectMake(117, 139, 93, 21)];
mylabel.text = @"Default text";
slider = [[UISlider alloc]initWithFrame:CGRectMake(30, 201, 252, 22)];
slider.maximumValue = 100;
slider.minimumValue = 1;
[slider addTarget:self action:@selector(changeLabelText) forControlEvents:UIControlEventValueChanged];
}
return self;
}

In the above code for the slider control I have set the max and min value properties by default the min value is zero and max value is one. Now into the loadView method add these views to your current view

- (void)loadView {
[super loadView];
[self.view addSubview:mylabel];
[self.view addSubview:slider];
}

Step 3: Now coming to our function named changeLabelText, here's how it looks

-(void)changeLabelText
{
mylabel.text = [NSString stringWithFormat:@"%.f",slider.value];
}

In the above code I have changed the text of the label as per the value of the slider since the value of the slider is a float value so I have used %.f in order to avoid the decimal part.

Step 4: In this step you have to select the AppDelegate.m file of your project and add this viewController subclass to your iPhone window, here's how its done

#import "SliderDemoAppDelegate.h"
#import "sliderviewController.h"

@implementation SliderDemoAppDelegate

@synthesize window;

- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Override point for customization after application launch
sliderviewController *sliderVC = [[sliderviewController alloc]init];
[window addSubview:sliderVC.view];
[window makeKeyAndVisible];
}

Step 5: Build and Go that means Run the application and you are done you will get the following output.





BitCode hopes that this post has helped you in clearing your concepts regarding the view and view controllers. You can post your queries at bitcode.pune@gmail.com for any technical assistance.

Segment control

In this post I will explain you how to display a simple segmented control on to the iPhone screen with some basic functionalities.

Design Phase:
I will take a segmented Control and a textview and on the hit of the segmented control I am trying to change the text of the textview. So here's how it's done. Here's how our final design will look like.



Step 1:
Create a window based application and add a class file to it which inherits from UIViewController with any name of your choice (I have given segmentedviewController as the name of my class file).


Step 2:
In this step we will design our application, first we will make the initial settings for our segmented Control and then we will go for the textView object but before doing that we have to declare the objects of these class into the .h file.



The above code has a function named changeTextonSegmentSelect which will change the text of the textView and you can see that it takes a parameter which is the object of UISegmentedControl this is because I wanted to chane the text based upon the selected index of the segmented control.



and now lets go to the .m file where we will get the init method and we can do some coding



@implementation segmentedviewController

// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
NSArray *items = [[NSArray alloc]initWithObjects:@"One",@"Two",@"Three",@"Four",@"Five",nil];
segmentcontrol_object = [[UISegmentedControl alloc]initWithItems:items];
[segmentcontrol_object setFrame:CGRectMake(53, 74, 207, 44)];
segmentcontrol_object.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentcontrol_object addTarget:self action:@selector(changeTextonSegmentSelect:) forControlEvents:UIControlEventValueChanged];
[items release];
txtView = [[UITextView alloc]initWithFrame:CGRectMake(41, 155, 230, 238)];
[txtView setFont:[UIFont fontWithName:@"Georgia" size:30]];
txtView.textAlignment = UITextAlignmentCenter;
txtView.text = @"Default Text";
}
return self;
}




In the above code you can see that in the start I have took an NSArray this is because the segmentedControl has a method called initWithItems which takes an object of NSArray as its argument.

After that I have made the settings and adjusted the frame of both the controls. If you read the code the code is pretty much self explanatory.

Now lets go to the loadView method and add these controls to your current view



- (void)loadView {
[super loadView];
[self.view addSubview:segmentcontrol_object];
[self.view addSubview:txtView];
}


Step 3: Now lets give body to our method called changeTextonSegmentSelect and it looks like this


-(void) changeTextonSegmentSelect:(UISegmentedControl*)sender;


So lets see the code to this method into the .m file of segmentedviewController




In the above code I have used swithc case inorder to check which index of the segment control was selected by the user and on the basis of that selection I have changed the text of the textView.

Now the final part is to add this view into the iPhone window so for this we will go into the AppDelegate.m file of our project and add this code to your AppDelegate.m file and you are done

















Step 4:
Press Build and Go to run the application and when you do that the application will load in the iPhone simulator and you will get the below output






BitCode hopes that this post has helped you in clearing your concepts regarding the view and view controllers. You can post your queries at bitcode.pune@gmail.com for any technical assistance.

UIPickerView

In this post we will have a look at UIPickerView.

Design Phase: In this application what we will do is print the current value of the UIPickerView in the UILabel, here's a look at our final view



Step1: Open the xcode and create a window based application and add UIViewController subclass file with the name pickerviewController. In the .h file of the pickerviewController create the object of UIPickerView class and UILabel, also create two object of NSArray class so now your code should look like this

#import


@interface pickerviewController : UIViewController {

NSArray *months;
NSArray * number_array;
UILabel *monthlabel,*numberlabel,*currentMonthLabel,*currentNumberLabel;
UIPickerView *pickerView;
}

@end


In the above code I have took two objects of NSArray class so that I can add the values of those NSArray as a title for my picker object.

Now you must have seen a weird line besides the UIViewController well those are nothing but protocols of the UIPickerView class which help the UIPicker to do some basic functionality like setting the number of components, setting the title for the rows of UIPicker etc.

Step 2: Go to the pickerviewController.m file and select the init method where you have to set the frame of the UIPickerView and the rest of the labels also you have to specify the location of the dataSource and Delegate method of the UIPicker controller that you will be using , so here’s how it looks

@implementation pickerviewController


// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
months = [[NSArray alloc]initWithObjects:@"Jan",@"Feb",@"Mar",@"Apr",@"May",@"Jun",@"Jul",@"Aug",@"Sep",@"Oct",@"Nov",@"Dec",nil];
number_array = [[NSArray alloc]initWithObjects:@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",nil];
//setting the frame of the UIPickerView
pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, 320, 216)];
//setting the frame of the labels
currentMonthLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 313, 114, 23)];
currentNumberLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 344, 128, 23)];
monthlabel = [[UILabel alloc]initWithFrame:CGRectMake(179, 314, 86, 21)];
numberlabel = [[UILabel alloc]initWithFrame:CGRectMake(179, 346, 86, 21)];

//setting the text of the labels
currentMonthLabel.text = @"Current Month";
currentNumberLabel.text = @"Current no";
monthlabel.text = @"Month";
numberlabel.text = @"Number";
//setting the dataSource and delegate properties of the UIPickerView
pickerView.dataSource = self;
pickerView.delegate = self;
//show pickers selection Indicator
pickerView.showsSelectionIndicator = YES;
}
return self;
}


Now into the loadView method we add these views to our current view

- (void)loadView {
[super loadView];
[self.view addSubview:currentMonthLabel];
[self.view addSubview:currentNumberLabel];
[self.view addSubview:monthlabel];
[self.view addSubview:numberlabel];
[self.view addSubview:pickerView];
}



Step 3: It’s time to see those delegate and datasource methods of UIPickerView for this go to the API reference and type UIPickerView in the search bar now you can see the UIPickerViewDatasource and Delegate protocols select any one of them in order to see their methods.

From the dataSource you have to select two methods that’s the numberOfComponentsInPickerView and pickerViewnumberOfRowsInComponent,
The first method that’s numberOfComponentsInPickerView will determine the number of parts that must be present in the UIPickerView and the second method pickerViewnumberOfRowsInComponent determines the number of rows that must be present in the components, lets see the implementation of those methods

//Pickers datasource method

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{
return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{
if(component==0)
{
return [months count];
}
else if (component ==1)
{
return [number_array count];
}
return 0;
}



Now I have added two delegate methods for UIPickerView and the name of those methods are
pickerViewtitleForRowforComponent this method will set the title for the UIPickerView’s rows and the other delegate method called pickerView didSelectRow inComponent which performs an action whenever a particular component of the UIPickerView is selected so lets see the implementation of these methods

//Pickers Delegate Method


- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

{
if (component ==0)
{
return [months objectAtIndex:row];
}
else if(component ==1)
{
return [number_array objectAtIndex:row];
}
return @"abc";
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if(component==0)
{
monthlabel.text = [months objectAtIndex:row];
}
else if(component ==1)
{
numberlabel.text = [number_array objectAtIndex:row];
}
}



Step 4: Your all done by now just go to the AppDelegate.m file of your project and add this view to your window here’s how you do it

#import "PickerDemoAppDelegate.h"
#import "pickerviewController.h"

@implementation PickerDemoAppDelegate

@synthesize window;


- (void)applicationDidFinishLaunching:(UIApplication *)application {

// Override point for customization after application launch
pickerviewController *pickerVC = [[pickerviewController alloc]init];
[window addSubview:pickerVC.view];
[window makeKeyAndVisible];
}



Step 5: Build and Go, run the application and your all done, you will get the following output



BitCode hopes that this post has helped you in clearing your concepts regarding the view and view controllers. You can post your queries at bitcode.pune@gmail.com for any technical assistance.