Code source de l’application Clock pour iPhone proposé dans un tutoriel OVH

Code source de l’application Clock pour iPhone proposé dans un tutoriel OVH

Il y a trois mois, OVH proposait sur son site un tutoriel aidant à développer sa première application iPhone. Le tutoriel en question avait pour objectif le développement d’une application permettant d’afficher l’heure au centre de l’écran sur un fond noir. Rien d’extraordinaire de premier abord, mais la complexité de prise en main de l’IDE d’Apple (XCode) donne à ce tutoriel son intérêt puisqu’il permet de démarrer en douceur en se familiarisant avec différentes de ses fonctionnalités.

Malheureusement, le code source du tutoriel n’est pas fourni, je vous propose donc de le retrouver sur GitHub, et de le consulter tout au long de la lecture du tutoriel.

L’url du repo GitHub: https://github.com/akinsella/clock

L’url du tutoriel sur le site d’OVH: http://www.ovh.com/fr/all/a494.creer_sa_premiere_application_iphone

  • Le fichier ‘ViewController.h’
//
//  ViewController.h
//  Clock
//
//  Created by Alexis Kinsella on 09/06/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import <uikit /UIKit.h>

@interface ViewController : UIViewController {
    UILabel *hoursAndMinutesLabel;
    UILabel *secondsLabel;
}

@property (nonatomic, retain) IBOutlet UILabel *hoursAndMinutesLabel;
@property (nonatomic, retain) IBOutlet UILabel *secondsLabel;

@property (nonatomic, retain) NSDateFormatter *dateFormatter;
@property (nonatomic, retain) NSTimer *walkTimer;



- (void)updateClock;
   
@end
  • Le fichier ‘ViewController.m’
//
//  ViewController.m
//  Clock
//
//  Created by Alexis Kinsella on 09/06/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ViewController.h"

@implementation ViewController

@synthesize hoursAndMinutesLabel;
@synthesize secondsLabel;
@synthesize walkTimer;
@synthesize dateFormatter;

-(void)updateClock {
    NSDate *currentDate = [[NSDate alloc] init];
   
    [self.dateFormatter setDateFormat:@"hh:mm"];
    self.hoursAndMinutesLabel.text = [self.dateFormatter stringFromDate:currentDate];
    [self.dateFormatter setDateFormat:@"ss"];
    self.secondsLabel.text = [self.dateFormatter stringFromDate:currentDate];
   
    // ARC forbids explicits release  
    // [currentDate release];
}

- (void) viewWillAppear:(BOOL)animated {
    NSTimer *timer = [NSTimer timerWithTimeInterval:1.0
                                             target:self
                                           selector:@selector(updateClock)
                                           userInfo:nil repeats:YES];
    [self setWalkTimer: timer];
   
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
   
    [self updateClock];
   
    [super viewWillAppear:animated];
}

- (void) viewDidDisappear:(BOOL)animated {
    [[self walkTimer] invalidate];
    [self setWalkTimer:nil];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    NSDateFormatter *df = [[NSDateFormatter alloc] init];
   
    [self setDateFormatter: df];
   
    // [dateFormatter release];
   
    [self.dateFormatter setLocale:[NSLocale autoupdatingCurrentLocale]];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
   
    self.dateFormatter = nil;

    // Release any retained subviews of the main view.    
    self.hoursAndMinutesLabel = nil;
    self.secondsLabel = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

@end
  • Le fichier ‘AppDelegate.m’
//
//  AppDelegate.m
//  Clock
//
//  Created by Alexis Kinsella on 09/06/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "AppDelegate.h"

#import "ViewController.h"

@implementation AppDelegate

@synthesize window = _window;
@synthesize viewController = _viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationNone];
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end

Leave a Reply