Archive for the ‘Tutorial’ Category

Using the UIButton in Xcode

3. July 2011

No Comments »

HTML5 Speech Input

4. June 2011

3 Comments »

The new features being introduced by the HTML5 standard are simply astounding. Just as the awe of one feature starts to wear off, I discover another that is even more awesome than the last. The current jaw dropping discovery is largely due to the Speech Input API Specification. Thats right, speech input.

You can add speech input to any input element by simply using the x-webkit-speech attribute.

The following code
[code]
[/code]
Will yield this

You can do other exciting actions language translations, or the famous voice search for android:
[code]

[/code]

Although this currently only works in Chrome 11 and relies on Google’s voice API, it is anticipated that more browsers will adopt the technology, and users will have the ability to choose which voice API they want to use.

*does the happy dance*

Hello World in Xcode 4.0

17. May 2011

No Comments »

We will start off by creating a new project and selecting a Window-based Application.

And naming it HelloWorld

Next we want to create a view controller. Go to File > New > New File… and select UIViewController subclass.

A UIViewController subclass works by overriding functions in its base class. The function of particular interest to us is the loadView. This is where we will add controls to our view.

[code lang=cpp]- (void)viewDidLoad
{
//Create a frame
CGRect frame = CGRectMake(0, 0, 320, 480);

//Initialize the view with our frame
self.view = [[UIView alloc] initWithFrame:frame];

//Set the background color of the view to be white
self.view.backgroundColor = [UIColor whiteColor];

[super viewDidLoad];
}[/code]

But before we add controles to the view, we need to create a frame which will hold the view. CGRect frame = CGRectMake(0, 0, 320, 480); creates a frame starting at point (0, 0) (the upper left corner) with a width of 320 pixels by 480 pixels (the size of an iPhone screen). We then allocate and initialize the view with the frame we just created. Now that we have a view, we can add our label.

[code lang=cpp]- (void)viewDidLoad
{
CGRect frame = CGRectMake(0, 0, 320, 480);
self.view = [[UIView alloc] initWithFrame:frame];
self.view.backgroundColor = [UIColor whiteColor];

//Create a frame to hold the label
frame = CGRectMake(50, 50, 100, 50);

//Create and initialize the label
UILabel *label = [[UILabel alloc] initWithFrame:frame];

//Set the labels text to Hello, World!
label.text = @"Hello, World!";

//Add the label to the view
[self.view addSubview:label];

//Release the reference
[label release];

[super viewDidLoad];
}[/code]

Like before, we start by creating a frame to hold the label. Once the frame is created, we can allocate a UILabel, initialize it, set its text, and add it to the view. Lastly, since Objective-C uses reference counting, it is a good idea to release the label to avoid any memory leaks – as a general rule of thumb, if you use a * make sure you release it.

Now that we have a view controller with a label, we need to add the view controller to the window. In your HelloWorldAppDelegate.h start by forward declaring the HelloWorldViewController class – this will tell the app delegate the HelloWorldViewController class exists. Then create an instance variable of type HelloWorldViewController, and finally make it a property.

[code lang=cpp]#import
//Forward declare the class
@class HelloWorldViewController;

@interface HelloWorldAppDelegate : NSObject {
//Create an instance variable (don't forget to release this)
HelloWorldViewController *viewController;
}

@property (nonatomic, retain) IBOutlet UIWindow *window;

//Create a property
@property (nonatomic, retain) HelloWorldViewController *viewController;

@end[/code]

Finally, in HelloWorldAppDelegate.m import the HelloWorldViewController.h file.
[code lang=cpp]#import "HelloWorldAppDelegate.h"

#import "HelloWorldViewController.h"[/code]

Synthesize the viewController instance variable

[code lang=cpp]@synthesize window=_window;
@synthesize viewController;[/code]

Then finally, allocate a HelloWorldViewController and add to the window viewController.view as a subview.

[code lang=cpp]- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.viewController = [HelloWorldViewController alloc];
[self.window addSubview:self.viewController.view];

// Override point for customization after application launch.
[self.window makeKeyAndVisible];
return YES;
}[/code]

Be sure to release viewController when you are done with it!

[code lang=cpp]- (void)dealloc
{
[viewController release];
[_window release];
[super dealloc];
}[/code]

Build and Go!