Monday, 4 April 2011

Drawing a Rectangle

In this post we will learn how to create geometric views in the iPhone.

Step 1: Open Xcode select the windows based application template give your project an appropriate name, after doing this add UIViewController subclass file to your application. In iPhone if we want to draw some shapes or perform any sort of drawing stuff then in that case you have to take a separate view and then add this view to your view controller class, you may follow this mechanism not only for drawing geometric views but to create your custom views like a login control, etc. So now after adding UIViewController subclass file add UIView subclass file and name it Rectangle, since it’s the rectangle shape that we will be drawing first,









Step 2: Select the Rectangle.m file and inside that file you will find a method called as drawRect, the drawRect method allows the programmer to create his/her own custom views, so in this case I just want a small rectangle to appear in my view so for that add the following code in the drawRect method

- (void)drawRect:(CGRect)rect {

// Drawing code

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, 3.0f);

CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);

CGContextAddRect(context,CGRectMake(80, 80, 120, 120));

CGContextStrokePath(context);

}

Code Explanation:

CGContextRef : It represents a drawing environment where the developer can draw his views. The drawRect method takes a parameter called as rect and this method gets called directly, so now when you will create the object of this method in the myviewController.m file and pass the parameter of frames to the init method of the rectangle class this frame will be referred to as the current context and inside the current context you can draw anything

UIGraphicsCurrentContext(): It represents the current graphics context.

CGContextSetStrokeColorWithColor: This method will set the color for the context

CGContextAddRect: This method will add a rectangle path in the current context, it takes two parameter one is the context in which you want to draw the rectangle and the other is the frame of the rectangle like its height, width, x and y.

CGContextStrokePath: This method will paint a line along the given path in the context.

Step 3: Select the myviewController.h file and declare the object of the rectangle class

#import

#import "Rectangle.h"



@interface myviewController : UIViewController {

Rectangle *r;

}


@end


and now traverse to the myviewController.m file and select the loadView method and add this piece of code

- (void)loadView {

[super loadView];


r = [[Rectangle alloc]initWithFrame:CGRectMake(20, 82, 280, 348)];

[self.view addSubview:r];

}



Code Explanation: The above code just adds the rectangle view to the current view.

Step 4: Select the appDelegate.m file and add the myviewController view to the iPhone window

#import "DrawingShapesAppDelegate.h"

#import "myviewController.h"

@implementation DrawingShapesAppDelegate


@synthesize window;


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


// Override point for customization after application launch

myviewController *obj = [[myviewController alloc]init];

[window addSubview:obj.view];

[window makeKeyAndVisible];

}

Step 5: Press build and go to get the output just like below



If you want to fill in this rectangle with color then in that case you can update the drawRect method and add this two lines

- (void)drawRect:(CGRect)rect {

// Drawing code

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextSetLineWidth(context, 3.0f);

CGContextAddRect(context,CGRectMake(80, 80, 120, 120));


CGContextSetFillColorWithColor(context, [UIColor blueColor].CGColor);

CGContextFillPath(context);

}


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

Tuesday, 29 March 2011

Tabbar Controller

In this post we will have a look at UITabBarController, there is also a class called as the UITabbar but mostly I prefer UITabBarController, TabbarController is used to control the Tabbar view and is mostly used to switch from one view to the other view.

Design Phase: In this post I am having two views one is a label view and the other is a table view, here’s how the final output will look like.



Step 1: In some of my earlier post I have explained how to show a simple label and a table so you can refer them if you like.

Step 2: For this tabbar application we will require two views so you can have the firstview as the subclass of the UIViewController which will be for the label and your second view will be the subclass of the UITableViewController which will be for the table . Make the settings for both the views by referring my earlier posts .

Step 3: Now go to the AppDelegate.m file and import both the viewControllers and create the object of both the viewControllers.

#import "TabbarDemoAppDelegate.h"
#import "firstviewController.h"
#import "secondviewController.h"

@implementation TabbarDemoAppDelegate

@synthesize window;

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

// Override point for customization after application launch
firstviewController *firstVC = [[firstviewController alloc]init];
secondviewController *secondVC = [[secondviewController alloc]initWithStyle:UITableViewStylePlain];
NSArray *objectList = [[NSArray alloc]initWithObjects:firstVC,secondVC,nil];
UITabBarController *tabbarC = [[UITabBarController alloc]init];
tabbarC.viewControllers = objectList;
[window addSubview:tabbarC.view];

[window makeKeyAndVisible];
}



Code Explanation:

Now the best part about TabbarController is that it has a viewController property, which takes the array of the viewController objects.

So I have took an NSArray and added the objects of the viewController to it and ultimately using the viewController property of the UITabbarController I have supplied the data of the views that will be appearing on the tabbar

so once you have done this now you can add the tabbarController view to the window as all the information about the view is contained in the tabbarcontroller object and then press build and go to 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.

switch control

In this post I will demonstrate on how to use a switch control, The switch control is just like a switch with an on and off text and one more thing you have to keep in mind before we proceed and that is the width and height of the switch will always be 94 and 27 not more than that, so let's begin

Design Phase: In this scenario when the switch displays off the label text must also change to off and when it displays on the label text must display on pretty simple right

Step 1: Open Xcode and create a window based or a view based application and give it an appropriate name, I normally am a faculty and I give the name of my projects like the control name and with the prefix demo. The name of this project that I gave was SwitchDemo. For designing we need a label and a switch control and a function with the help of which we will change the text of the label.

Step 2: Now add a UIViewController subclass file to your project and give it the name swithcdemoviewController. Select the .h file and add this piece of code to your project

@interface switchdemoviewController : UIViewController {
UILabel *lbl;
UISwitch *objSwitch;

}
-(void)changeLabelText;

@end

Now go to the .m file of the switchdemoviewController and select its init method where you will set the frame of both label and the UISwitch object here's how you will do that

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
lbl = [[UILabel alloc]initWithFrame:CGRectMake(86, 199, 202, 21)];
lbl.text = @"Default Text";
objSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(80, 122, 94, 27)];
[objSwitch addTarget:self action:@selector(changeLabelText) forControlEvents:UIControlEventValueChanged];
}
return self;
}

Into the loadView method add these objects to your view

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

Step 3: In this step we will give body to our function named chanegLabelText wich looks like this

-(void)changeLabelText
{
if(objSwitch.on)
{
lbl.text = @"The switch is on";
}
else
{
lbl.text = @"The Switch is off";
}
}

In the above code I have just checked a condition whether the switch is on or not and if it is on then change the label text to on else change the label text to off, all this is being done with the help of the property that the switch control has named on.

Step 4: Add this view (switchdemoviewController) to your window for that select the AppDelegate.m file of your project, my code looks like this

#import "SwitchDemoAppDelegate.h"
#import "switchdemoviewController.h"

@implementation SwitchDemoAppDelegate

@synthesize window;


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

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

Step 5: Build and Go means run the application you are 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.