Displaying date from a date picker in xCode

April 8, 2011 § Leave a comment

Our professor, Dan, introduced us to xCode, Interface Builder and the iOS Simulator in one of our last few Multimedia Pioneering classes. These are the main development tools for the IOS platform. xCode is a code editor also known as IDE (Integrated Development Environment), which is quite popular amongst the Mac users and is normally used to develop applications. Interface Builder allows us to create interfaces visually. There are built-in buttons, sliders and tab-bars etc that can be dragged onto your application interface. They can be customized to your preference, through color palettes and different panels.

iOS applications are developed in objective-C using the Cocoa framework (it is Apple Inc.’s API for the Mac OS X operating system). iOS applications have an AppDelegate, which acts as the main application class. The various screens of the application viewed by the user are comprised of UIViewControllers that manages several views. The classes folder consists of “.h” file, as well as, “.m” files. “.h” file is where we declare all our methods and properties, and are then integrated in the “.m” files.

In class, we decided to display a date from a built-in component, date picker (myPicker), on to a textfield (myLabel).  We open up DateViewController.h file, which is located in the classes folder, and add the properties and instance variables that we are going to use for our application. Comments are in red color and code is in black (Bold).

//We start off by importing the ukit.h file, which is a user interface kit that is responsible for providing all the visual graphics for the iPhone, such as, windows, text boxes, multi-touch gestures etc.

#import <UIKit/UIKit.h>

// * is a syntax to declare a variable. In this case, we have “*myLabel” for the UILabel and “*myPicker” for UIDatePicker.

//@interface says that this is a declaration of the class viewViewController.  The “:” (colon) specifies the superclass.

//IBOutlet – When you create an object to be visible, you use the keyword IBOutlet, for example: IBOutlet UILabel *myLabel;

@interface viewViewController : UIViewController {

IBOutlet UILabel *myLabel;

IBOutlet UIDatePicker *myPicker;

}

//The @property declares the property.

//The “retain” in the parenthesis specifies that the setter should retain the input value.

@property (nonatomic, retain) IBOutlet UILabel *myLabel;

@property (nonatomic, retain) UIDatePicker *myPicker;

 

// ” -”  A single dash before a method name means it’s a instance method.

//setDate method is added and needs to be tagged as an IBAction, in order for it to be recognized by the interface builder. IBAction alerts Interface Builder that “setDate” is an action for target/action connections. IBAction could also be known as an action method or “private function” in the as3 script.

– (IBAction)setDate:(id)sender;

//@end symbol ends the class declaration.

@end

We then open our DateViewController.m, which is known as the implementation file. It contains the actual code for the methods. Here we will add the following code..

@implementation viewViewController

//@synthesize tells the compiler to create accessor methods for each @property declaration. In this case, it created for myLabel and myPicker.

@synthesize myLabel, myPicker;

DateViewController.m has many methods that are commented out and are not necessary for what we are doing. The only one we need to uncomment is the viewDidLoad method.

//ViewDidLoad method is from our viewController class. This method is called, whenever the view is loaded.

– (void)viewDidLoad {

[super viewDidLoad];

//NSLog is like trace in as3 script.

NSLog(@”Hi from viewViewController”);

}

– (IBAction)setDate:(id)sender {

//NSlog logs messages to the console in Objective-C . The NSLog() function adds %@ token for objects. The NSLog function calls the description method on the object, and prints the NSString which is returned.

NSLog(@”date picked = %@”, myDate);

myLabel.text = myDate;

// NSDate inherits from: NSObject. It stores a date and time that can be compared to other dates and times. In this case, it stores the date in the myPicker and adds it to myDate2. This will display the date the user has picked and it displays it on the label field (myLabel.text).

NSDate *myDate2 = myPicker.date;

myLabel.text = myDate2.description;

 

Links

The links provided at the bottom are researched very carefully. If you go through these as I have them listed, it will definitely help you learn what you need for app development. Some of them do overlap a bit but provides a lot of detail.

1) This link is one of the best ones I could find. It is a pure theory based website that provides us with an overview on not only xCode and interface builder but also what programming and objective-C is. It is also goes through the MVC (Model, View, Controller) framework. It is a good refresher for people who are getting back into programming again and will pick it up quickly.

http://designthencode.com/scratch/

2) Although Dan went through the steps with us in detail, but this website allows us with a step by step process, starting from, how to register with Apple, to installing xCode, and learning all the fundamental tools of app development.

http://mobile.tutsplus.com/series/beginning-ios-development/

3) Here is a good website, that provides us with a good visual walkthrough of xCode and Interface builder.

http://www.ifans.com/forums/showthread.php?t=137183

4) Here is a good tutorial that specifically goes through creating To-Do list with SQL lite. The reason I have added this tutorial is because we have looked briefly into SQL lite (database engine) in our Application Development 2 class.

http://www.xcode-tutorials.com/creating-a-to-do-list-using-sqlite-part-1/

5) Wow, such a cool link. This guide allows us to convert our single-language applications into multilingual application. A replacement of country code is needed into the resource files. I think you will understand what I mean, once you read this article.

http://www.icanlocalize.com/site/tutorials/iphone-applications-localization-guide/#1

6) I just wanted to add one more website to the list, which I feel will be helpful in organizing your workspace. This website also provides us with fourteen essential tips, tricks, how-tos and resources needed for iPhone development in xCode.

http://mobileorchard.com/14-essential-xcode-tips-tricks-and-resources-for-iphone-devs/












Where Am I?

You are currently viewing the archives for April, 2011 at my IMM's blog.