How to display current date and time in iPhone?


Step 1: Open the Xcode, Create a new project using Single View Application. Give the application
“CurrentDateTime”.
Step 2: Need to add new viewController class in the project. Select project -> New file ->
UIViewController subclass -> next -> Give the application name “CurrentDateTimeView”.
Step 3: Now open the AppDelegate.h file and make the following changes:
#import <UIKit/UIKit.h>
@class CurrentDateTimeView;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
CurrentDateTimeView
 *currentDateTimeView;
}
@property (nonatomic, retain)IBOutlet CurrentDateTimeView *currentDateTimeView;
@property (strong, nonatomic) UIWindow *window;
@end
Step 4: In the AppDelegate.m file make the following changes:
#import "AppDelegate.h"
#import "CurrentDateTimeView.h"
@implementation AppDelegate
@synthesize window = _window,currentDateTimeView;
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
*)launchOptions
{
self.window
 = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor
 = [UIColor whiteColor];
[self.window makeKeyAndVisible];
currentDateTimeView
 = [[CurrentDateTimeView alloc]
initWithNibName
:@"CurrentDateTimeView"
bundle
:nil];
[_window addSubview:currentDateTimeView.view];
return YES;
}
@end
Step 5: Now open the “CurrentDateTimeView.h” file and make the following changes:
#import <UIKit/UIKit.h>
@interface CurrentDateTimeView : UIViewController
{
IBOutlet UIButton
 *button;
NSString *string;
IBOutlet UILabel
 *dateLabel;
}
@property (nonatomic, retain) IBOutlet UIButton *button;
@property (nonatomic, retain) IBOutlet UILabel *dateLabel;
@property (nonatomic, retain) NSString *string;
-(IBAction)currentdate:(id)sender;
@end
Step 6: Double click the CurrentDateTimeView.xib file and open it to the Interface Builder. First
drag the UIButton and UILabel from the library and place it to the view window. Select the button
from the view window and bring up connection inspector and connect touch up inside to the File’s
Owner icon and select “currentdate:” method and connect the File’s owner icon to the label and
select “dateLabel”. Now save the .xib file, close it and go back to the Xcode.
Step 7: In the CurrentDateTimeView.m file make the following changes:
#import "CurrentDateTimeView.h"
@implementation CurrentDateTimeView
@synthesize button, string, dateLabel;
-(IBAction)currentdate:(id)sender
{
NSDatedate = [NSDate date];
NSDateFormatterformatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:MM:SS"];
NSStringstr = [formatter stringFromDate:date];
[dateLabel setText:str];
}
(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self
 = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
(void)didReceiveMemoryWarning
{
// Releases the view if it doesn’t have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren’t in use.
}
#pragma mark – View lifecycle
(void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
(void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
Step 8: Now compile and run the application on the Simulator.

Popular posts from this blog

How to use nsxmlparser in Iphone SDk?

How to draw a pie chart using iPhone sdk?

How to create Singleton in iPhone?