Constructing the iOS App for a Beginner’s Consideration

The beginners in the development realm always have a hard time dealing with certain elements that are convoluted, and justifiably so. Those running a business invest to prefer iPhone Development Services, but individuals are leaned towards learning.

So, it is always a great idea to get a clear understanding of the basics and then take that knowledge forward and leap it into something highly sophisticated technology wise.

So, for all the novices out there, here are 3 easy steps you can adopt and implement to create something that is custom:

ios-courses

main.m is Where Most of the Magic Happens

The main.m file contains main() – and main() and that’s where the process begin with. So, to start of the project, we write the code for main.m:

main.m
#import <UIKit/UIKit.h>

int main(int argc, char *argv[]){
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @”HelloWorldAppDelegate”);
[pool release];
return retVal;
}

So, an autorelease pool is creating at the beginning and it is duly released later after it makes calls to the UIApplicationMain(). We define this function as follows:

UIApplication.h inside the UIKit.framework wrapper
int UIApplicationMain(int argc, char *argv[], NSString *principalClassName,
                                                                         NSString *delegateClassName)

While the initial two parameters are meant to arguments, the third parameter gives a name to the application class. And the fourth parameter is what specifies the name of the application delegate class.
UIApplicationMain() essentially does the job of instantiating the application and the application delegate objects. This is followed by setting the property of the application object for the application delegate instance. Any kind of event that takes place is then recorded b the system and queued, until the application object dequeues the events.

And Then You Have to Set Up the Application Delegate Class
This one represents a standard application delegate class:
HelloWorldAppDelegate.h
#import <UIKit/UIKit.h>
@class MyView;
@interface HelloWorldAppDelegate : NSObject <UIApplicationDelegate> {

UIWindow *window;
MyView   *view;
}
@end

UIApplicationDelegate protocol is used by the the application delegate class and it is the instance variables that store the references to the objects for user-interface.
And then we move to the applicationDidNotFinishLaunching method that is contained by UIApplicationDelegate protocol. Application object invokes this method essentially and its purpose is to communicate to the delegate thhat app has completed its launching process. The following code illustrates how the application delegate class is implemented and in the DidFinishLaunching method, the main window is created:

HelloWorldAppDelegate.m
#import “HelloWorldAppDelegate.h”
#import “MyView.h”
@implementation HelloWorldAppDelegate
– (void)applicationDidFinishLaunching:(UIApplication *)application {
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CGRect frame = [UIScreen mainScreen].applicationFrame;
view = [[MyView alloc] initWithFrame:frame];
view.message = @”Hello World!”;

view.backgroundColor = [UIColor whiteColor];
[window addSubview:view];
[window makeKeyAndVisible];
}
– (void)dealloc {
[view release];
[window release];
[super dealloc];
}

This is how it all works: Windows are instances of UIWindow, which happens to be a subclass of UIView. UIView is the root level class and this is the class which handles the gestures by the users. The dimensions of the windows object are defined by the initializer. Instance of MyView is initialized and it is added as a subview to the window.

We Then Move to Creating the Interface Subclasses

A subclass of UIView is defined and then its event-handling is override in order to acknowledge the events initiated by the user. We need to open the Xcode and go to File>New File. From the window, we will select UIView subclass.

new_file_custom_view-1024x710

Now, the file you have created will be named as MyView.m and click on the “finish” button:

new_file_custom_view_2-1024x631

We will declare MyView by the following code:
#import <UIKit/UIKit.h>
@interface MyView : UIView {
NSString *message;
}
@property(nonatomic, retain) NSString *message;
@end

When we need to draw, we use a font to draw the message text. As soon as the user separates the finger from the screen, the event is created and then it is stored in the application queue. The application object channels the event to the main window which recieves the event by subviews. With our code example, the events are received via the touchesEnded:withEvent: method.

MyView.m

#import “MyView.h”

@implementation MyView
@synthesize message;

– (void)touchesEnded: (NSSet *)touches withEvent:(UIEvent *)event{
if( [(UITouch *)[touches anyObject] tapCount] == 2){
// handle a double-tap
}
}
– (void)drawRect:(CGRect)rect{
[message drawAtPoint:CGPointMake(60,180)
withFont: [UIFont systemFontOfSize: 32]];
}

– (void)dealloc{
self.message = nil;
[super dealloc];
}
@end

helloworld

The above code thus gives its output like in the image.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s