In this application we will see how to Current time display in the application. So let see how it
will worked.
Step 1: Open the Xcode, Create a new project using View Base application. Give the application
“TimeApplication”.
Step 2: Xcode automatically creates the directory structure and adds essential frameworks to it. You can explore the directory structure to check out the content of the directory.
Step 3: Expand classes and notice Interface Builder created the ViewController class for you. Expand Resources and notice the template generated a separate nib, TimeApplicationViewController.xib for the TimeApplication.
Step 4: Open the TimeApplicationViewController.h file and make the following changes:
@interface TimeApplicationViewController : UIViewController {
IBOutlet UIButton *submit;
IBOutlet UILabel *time;
NSString *data;
}
@property (nonatomic, retain) IBOutlet UIButton *submit;
@property (nonatomic, retain) NSString *data;
@property (nonatomic, retain) IBOutlet UILabel *time;
-(IBAction)ShowTime:(id)sender;
@end
Step 5: Double click the TimeApplicationViewController.xib file and open it to the Interface Builder. First drag the Round Rect Button and label from the library and place it to the view window. Now select the Round Rect button and bring up Connection Inspector and connect Touch Up inside to the File’s Owner icon and select ShowTime: method. Select File’s Owner icon to the label and select time. Now save the .sib file close it and go back to the Xcode.
Step 6: Open the TimeApplicationViewController.m file and make the following changes:
@implementation TimeApplicationViewController
@synthesize submit, time, data;
-(IBAction)ShowTime:(id)sender
{
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[[NSDateFormatter alloc]init]autorelease];
[formatter setDateFormat:@"HH:MM:SS"];
NSString* str =[formatter stringFromDate:date];
NSLog(str);
[time setText:str];
}
- (void)dealloc
{
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark – View lifecycle
- (void)viewDidUnload
{
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end
Step 7: Now Compile and run the application on the Simulator
You can Download SourceCode from here