Hi folks,
Since it has been a long time that you people did not get any post from my side,This is very first post about iPhone app development .
I assume you people are familiar with basic of objective c and cocoa framework and Xcode.
So this post bring you the anatomy of first HelloWorld application
let's start...
Prerequisites
1. Xcode
2.basic of objective c and iphone SDK
last and foremost patience :-)
Here are the following steps you need to build your HelloWorld application
1.Create new XCode Project
Create your project going through XCode ->File ->New Project and select Empty Application .Name the project HelloWorld and click Save as shown in figure
XCode will create two file AppDelegate.h and AppDelegate.m . Difference between .h and .m file is in objective c all declaration goes to .h file and implementation goes to .m file.
Now What is AppDelegate actually?
When you create your project, main.m file under Supporting files is generated by XCode
it looks like
This method is starting point for developer didFinishLaunchinWithOptions is application life cycle method it is called when application will launch (onCreate() in Android )
Since it has been a long time that you people did not get any post from my side,This is very first post about iPhone app development .
I assume you people are familiar with basic of objective c and cocoa framework and Xcode.
So this post bring you the anatomy of first HelloWorld application
let's start...
Prerequisites
1. Xcode
2.basic of objective c and iphone SDK
last and foremost patience :-)
Here are the following steps you need to build your HelloWorld application
1.Create new XCode Project
Create your project going through XCode ->File ->New Project and select Empty Application .Name the project HelloWorld and click Save as shown in figure
XCode will create two file AppDelegate.h and AppDelegate.m . Difference between .h and .m file is in objective c all declaration goes to .h file and implementation goes to .m file.
Now What is AppDelegate actually?
When you create your project, main.m file under Supporting files is generated by XCode
it looks like
#import <UIKit/UIKit.h>
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
as in any C program the execution of Objective -c application starts from main().the main() function starts by creating autorelease pool and ends by releasing it. In between it makes a call to the UiApplicationMain() function. UiApplicationMain() is declared as follows
int UIApplicationMain(int argc, char *argv[], NSString *principalClassName, NSString *delegateClassName);
The UiApplicationMain() instantiate the application and the application delegate objects. after that,it sets the delegate property of the application object to the application delegate instance.
The Typical application delegate class looks like
#import <UIKit/UIKit.h>
@interface AppDelegate : NSObject <UIApplicationDelegate>
{
}
@end
Notice that application delegate class inherits NSObject class and adopts UIApplicationDelegate protocol and implements the UIApplicationDelegate methods.One of the very first method of UiApplicationDelegate is
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{}
This method is starting point for developer didFinishLaunchinWithOptions is application life cycle method it is called when application will launch (onCreate() in Android )
Note : NSObject is the super class of all classes available in objective c and Protocol is here interface
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
above code simply illustrate that when application will launch a new window will be allocated by
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
and set its background color to white by
self.window.backgroundColor = [UIColor whiteColor];
at last make it visible.
2. Create User Interface using code
For your application very first object you need is appdelegate class which we have been seen above
now suppose you application has more than one screen so how will you create your screen(View)?
In iphone each screen is ViewController (in Android Activity).
For this create a simple class which is a subclass of UIViewController going through
right click on AppDelegate.h >>>newFile >>Cocoa Touch >> UiViewController subclass >>next.
Name the file and create it.
3. Create object of your newly created UIViewController and add it to the main Window.
modify AppDelegate.h file with following piece of code
#import <UIKit/UIKit.h>
#import "HelloWorldViewController.h"
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
HelloWorldViewController *helloWorldVC;
}
@property (strong, nonatomic) UIWindow *window;
@end
modify AppDelegate.m file
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// create viewcontroller object
helloWorldVC=[[HelloWorldViewController alloc]init];
//add it to the window
[self.window addSubview:helloWorldVC.view];
[self.window makeKeyAndVisible];
return YES;
}
what you have done so far is you have created HelloWorldViewController and added it to main window. now create label in your controller
4.Create UiLabel and add it to HelloWorldViewController
- Open HelloWorldViewController.h file and declare UILabel
#import <UIKit/UIKit.h>
@interface HelloWorldViewController : UIViewController
{
UILabel *label;
}
@end
- Open HelloWorldViewController.m file create label and set it's property
#import "HelloWorldViewController.h"
@implementation HelloWorldViewController
- (id)init
{
self = [super init];
if (self) {
label=[[UILabel alloc]initWithFrame:CGRectMake(100, 250, 200, 30)];
label.text=@"HelloWorld";
}
return self;
}
- Add label to ViewController
-(void)loadView
{
[super loadView];
[self.view addSubview:label];
}
5. Build and Run
Run your project by clicking run button (top left corner in XCode).
enjoy coding......
Feel free to comment .For any query mail me @ bpsingh216@gmail.com
Do you wanna develop your iPhone Apps? No problem, we have huge information about iPhone Apps. If you need any help, please visit http://www.taoteapps.com & learn more.
ReplyDelete