ios chapter 2 mvc

Upload: seethalakshmi-sethurajan

Post on 14-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/30/2019 Ios Chapter 2 Mvc

    1/6

    Nimap Infotech, B-204, Pawapuri Apts, Love Lane, Mumbai 400010. Tel.:- +91 986 935 7889 Email:- [email protected] Web:- www.nimapinfotech.com

    Basic Project Structure for iPhone FrameWork

    After having some fare knowledge about the Objective C language, we now start with the iPhone development.

    The best way to learn any thing is to take up a test case. We will start our learning from a test case.

    We will make an application, which will have four sections. Each section will display various components and

    concept of iPhone.

    In order to start developing iPhone app we require Xcode. So first click XCode icon. We start by selecting New

    Project from the File Menu of the Xcode. A screen shows up which displays options of various templates as shownbelow

  • 7/30/2019 Ios Chapter 2 Mvc

    2/6

    Nimap Infotech, B-204, Pawapuri Apts, Love Lane, Mumbai 400010. Tel.:- +91 986 935 7889 Email:- [email protected] Web:- www.nimapinfotech.com

    As you can see above there are lot of templates provided by the Xcode to start the respective project. Template

    helps us to start our app in proper way with lot of basic things directly done on our behalf.

    We will select View Based application, as UIView is basic component in iPhone development, which is used to

    display various user interfaces to the user.

    When you select choose button, you will be presented by the pop up window, where you give name to your project.

    For the application, which we are developing, we will give it name say FirstProject and click save.

  • 7/30/2019 Ios Chapter 2 Mvc

    3/6

    Nimap Infotech, B-204, Pawapuri Apts, Love Lane, Mumbai 400010. Tel.:- +91 986 935 7889 Email:- [email protected] Web:- www.nimapinfotech.com

    Once you select save, Xcode opens up your project as shown below

    We will see first left side section of Groups & Files, which explore out entire project created by the Xcode. We will

    expand Classes folder to see what all files get created.

  • 7/30/2019 Ios Chapter 2 Mvc

    4/6

    Nimap Infotech, B-204, Pawapuri Apts, Love Lane, Mumbai 400010. Tel.:- +91 986 935 7889 Email:- [email protected] Web:- www.nimapinfotech.com

    As you can see two classes are formed (four files each class in objective c has two files one for interface .h and

    other implementation .m), FirstProjectAppDelegate and FirstProjectViewController.

    The AppDelegate file captures the entire life cycle of the application. Its remains in the memory till the time

    application is active. All the OS related notifications are capture by this class.

    The application developed for iPhone follow MVC architecture. MVC stands for Model-View-Controller. This

    architecture is highly recommended for any big system. Under this architecture a system gets divided into three

    major sections namely Model, which keeps all the data of the application, which will be manipulated. View; which

    will displays various UI to the user to interact with the application as well as result of the interaction. And

    Controller, which controls the entire system such handling of various user interactions, implementation of business

    logic and so on.

    We will go in details of these classes later on, now lets expand the other sources folder.

  • 7/30/2019 Ios Chapter 2 Mvc

    5/6

    Nimap Infotech, B-204, Pawapuri Apts, Love Lane, Mumbai 400010. Tel.:- +91 986 935 7889 Email:- [email protected] Web:- www.nimapinfotech.com

    This folder consists two files; one is Prefix files, which is access by all the classes in the application. This is a

    header file basically one can defines macros out here. Other file is main class, which is the starting point of the

    application.

    The main class contains following lines of code

    #import

    int main(int argc, char*argv[])

    {

    NSAutoreleasePool * pool = [[NSAutoreleasePoolalloc] init];

    int retVal = UIApplicationMain(argc, argv, nil, nil);

    [pool release];

    return retVal;

    }

    We never change these lines of code. The first line states that we will import the UIKit class, which contains

    definition of all the user interface components of the iPhone.

    Every application has one pool associated with it. Pool basically keeps all the component of the application. When

    application dies system empty this pool. Application starts execution with UIApplicationMain(argc, argv, nil, nil);

    function call. This function tells OS to start the application notification to the delegate class and thus control is

  • 7/30/2019 Ios Chapter 2 Mvc

    6/6

    Nimap Infotech, B-204, Pawapuri Apts, Love Lane, Mumbai 400010. Tel.:- +91 986 935 7889 Email:- [email protected] Web:- www.nimapinfotech.com

    passed to the delegate class, which is present in the Classes folder.

    As we told you earlier that control is passed from main class to the delegate class. Lets first see interface file of thedelegate file, which declares the structure of the delegate class.

    Following is the code written in this file

    #import

    @classFirstProjectViewController;

    @interface FirstProjectAppDelegate : NSObject {

    UIWindow *window;

    FirstProjectViewController *viewController;

    }

    @property (nonatomic, retain) UIWindow *window;

    @property (nonatomic, retain) FirstProjectViewController *viewController;

    @end

    As we told you before, each class in the objective should be child of NSObject class. Thus

    FirstProjectAppDelegate inherits from the NSObject which one can find from the following line

    @interface FirstProjectAppDelegate : NSObject

    Thus after the class name we have to put colon : and than name of the class from which we want to derive. In

    this FirstProjectAppDelegate derives from NSObject.