iPhone Project 1


Background

  • This is the first iPhone app I wrote last year
  • There is not a lot of code!!!
  • Best way to do this is just copy the code in XCode as needed and follow the simple instructions below about creating the UI in Interface Builder

Requirements

  • Provide effective light from the iPhone in Darkness
  • On/Off Switch
  • No Fade Out/Dim
Specification
  • See screen below.

  • Label is White when torch is on (Yes this is a very simple torch!)
  • Label is black when torch is off
  • On Application start torch is off
  • Off/On button toggles the light
Test
  • None required at this stage (OK so my objective C needs to get improved before I start building unit tests first). I promise it will happen …. yeah yeah.

Code (Please Note was built with XCODE 3.0.1)

  • iPhone_TorchViewController.h

//  iPhone_TorchViewController.h
//  iPhone Torch
//
//  Created by Dom Millar on 31/08/09.
//  Copyright DOMSCODE 2009. All rights reserved.
//
#import
@interface iPhone_TorchViewController : UIViewController {
IBOutlet UILabel *torchlabel;
}
@property (retain,nonatomic) UILabel *torchlabel;
-(IBAction)buttonPressed:(id)sender;
@end
  • iPhone_TorchViewController.m (Important Bits)

#import “iPhone_TorchViewController.h”

@implementation iPhone_TorchViewController
@synthesize torchlabel;
-(IBAction)buttonPressed:(id)sender
{
NSString *buttonTitle = [sender titleForState:UIControlStateNormal];
if([buttonTitle compare:@"On"] == NSOrderedSame)
{
[torchlabel setBackgroundColor:[UIColor whiteColor]];
}
else
{
[torchlabel setBackgroundColor:[UIColor blackColor]];
}
}

- (void)dealloc {
[torchlabel release];
[super dealloc];
}
Ok so now to hook up to the UI build the code in XCode then open the XIB File. Add the UI elements to the main view like in the above screenshot (1 label with background black, 2 buttons). Then hooking up the outlets and action is easy. Two steps in fact:
1. Option click on the File Owner and drag from the label item to the actual big label on the view.
2. Click on each of the buttons and bring up the button connections. Click on the Touch Up Inside connection and drag that to the File Owner – selecting the button pressed action. Make sure you do this for both buttons.


Issues
  • Should make it a bit more funky (FlashLight – Version 2)
  • Using the title of the buttons to determine which one pressed is a bit dodgy – will see if we can improve this

Summary

Ok so this was an extremely simple applIcation and no test driven development (Curse you Dom) but was achieved in just over an hour and considering this is my first iPhone app – I am pretty happy. In terms of the process, it was all fairly basic but as we consider this series of Projects you’ll see things get more complex.

Dom

About these ads

One response to “iPhone Project 1

  1. Nice job Dom.
    I knew it was matter of time before you started your blog. I hate iPhones but I won’t mind reading bits-n-pieces of iPhone code on your blog.

    The theme is very cool too. Good luck and yes make it a lot more funky.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s