Pages

Labels

Monday, April 9, 2012

Starting with Xcode



            1.1 Introduction To Objective C:

            Objective-C is the primary language used for Apple's Cocoa API, and it was originally the main language on NeXT's NeXTSTEP operating system.

            1.1.1 Interfaces and implementations

            Objective-C requires that the interface and implementation of a class be in separately declared code blocks. By convention, developers place the interface in  header file and the implementation in a code file. The header files, normally suffixed .h, are similar to C header files while the implementation (method) files, normally suffixed .m, can be very similar to C code files.

            1.1.1 a) Interface

            The interface of a class is usually defined in a header file. A common convention is to name the header file after the name of the class, e.g.Ball.h would contain the interface for the class Ball.
            An interface declaration takes the form:
            @interface classname : super classname {
            // instance variables
            }
            + classMethod1;
            + (return_type)classMethod2;
            + (return_type)classMethod3:(param1_type)param1_varName;

            - (return_type)instanceMethod1:(param1_type)param1_varName :                        (param2_type)param2_varName;
            - (return_type)instanceMethod2WithParameter:(param1_type)param1_varName             andOtherParameter:(param2_type)param2_varName;
            @end

            In the above, plus signs denote class methods, or methods that can be called on the class itself (not on an instance), and minus signs denote instance methods, which can only be called on a particular instance of the class. Class methods also have no access to instance variables.
            1.1.1 b) Implementation
            The interface only declares the class interface and not the methods themselves: the actual code is written in the implementation file. Implementation (method) files normally have the file extension.m, which originally signified "messages".
            @implementation classname
            + (return_type)classMethod {
            // implementation
            }
            - (return_type)instanceMethod {
            // implementation
           
            }
            @end

           
            1.1.2 Instantiation

            Once an Objective-C class is written, it can be instantiated. This is done by first allocating an uninitialized instance of the class (an object) and then by initializing it. An object is not fully functional until both steps have been completed. These steps should be accomplished with a single line of code so that there is never an allocated object that hasn't undergone initialization (and because it is not advisable to keep the intermediate result since -init can return a different object than that which it is called on).
            Instantiation with the default, no-parameter initializer:
            MyObject *o = [[MyObject alloc] init];
           
            Instantiation with a custom initializer:
            MyObject *o = [[MyObject alloc] initWithString:myString];
           
            In the case where no custom initialization is being performed, the "new" method can often be used in place of the alloc-init messages:
            MyObject *o = [MyObject new];


1.1.3 Protocols
            Objective-C was extended at NeXT to introduce the concept of multiple inheritance of specification, but not implementation, through the introduction of protocols. This is a pattern achievable either as an abstract multiply inherited base class in C++, or as an "interface" (as in Java and C#). Objective-C makes use of ad hoc protocols called informal protocols and compiler-enforced protocols called formal protocols.
            An informal protocol is a list of methods that a class can opt to implement. It is specified in the documentation, since it has no presence in the language. Informal protocols often include optional methods, which, if implemented, can change the behavior of a class. For example, a text field class might have a delegate that implements an informal protocol with an optional method for performing auto-completion of user-typed text. The text field discovers whether the delegate implements that method (via reflection) and, if so, calls the delegate's method to support the auto-complete feature.
           
            1.1.4 Garbage collection
              Objective-C 2.0 provides an optional conservative, yet generational garbage collector. When run in backwards-compatible mode, the runtime turns reference counting operations such as "retain" and "release" into no-ops. All objects are subject to garbage collection when garbage collection is enabled. Regular C pointers may be qualified with "__strong" to also trigger the underlying write-barrier compiler intercepts and thus participate in garbage collection.[15] A zero-ing weak subsystem is also provided such that pointers marked as "__weak" are set to zero when the object (or more simply, GC memory) is collected.
            The garbage collector does not exist on the iOS implementation of Objective-C 2.0. Garbage Collection in Objective-C runs on a low-priority background thread, and can halt on user events, with the intention of keeping the user experience responsive.


1.2 Creating new View Based Application
                      

                        To launch Xcode, double-click the Xcode icon located in the /Developer/Applications            folder  Alternatively, go the quicker route and use Spotlight: Simply type Xcode into the search box and Xcode should be in the Top Hit position.

    Figure Shows Xcode welcome Screen




            
        Choose create a new Xcode project and you will navigate to next page as shown in figure
 1.2.2. In that screen you will see different types of applications such as View based, Window based,
Navigation based etc. Choose View based application and click next button.

        You will navigate to next screen as shown in figure 1.2.3, here you have to enter your project name and choose the device family such as iPhone, iPad or universal and click Next. 

         You have to choose the location in which your project is to be stored and click create button.
       



               






             Figure 1.2.2




           
           
             Figure 1.2.3








          In your Xcode project, in the left pane you will able to see headers, implementation and interface files. You can write codes in interface and implementation files. In the right pane, different types of objects are placed which will help the developer to design interface file (.xib file).
       
        Figure Shows Xcode Project window
 



            1.3  Learning header,implementation and interface files

            Header files are used to declare the methods and objects that we are going to used in our application. Header files can see in Xcode with extension .h  and Actions and outlets are also declare in header files. Developer can set property to particular object that he is going to use in particular application.

          Implementation files are used to define the actions. Implementation files consist of  default methods such as ViewDidLoad, ViewWillAppear,etc. Implementation files can be see with extension .m .

            Interface files are help the developer to make up the user interface look. The developer can place the objects in interface files and he can design the look of application. Interface files are seen in Xcode with extension .xib. Xib means Next Step Interface Builder and it is also known as Nib file.






           
            1.4 Learning about actions and outlets

            One of the first things you need to understand in iPhone programming is outlets and actions. If you are familiar with traditional programming languages such as Java or C#, this is a concept that requires some time to get used to — the concepts are similar, just that it is a different way of doing things. At the end of this section, you will have a solid understanding of what outlets and actions are and how to create them, and be on your way to creating great iPhone applications.
            Actions are defined as IBAction and outlets are defined as IBOutlet. Developer have to connect actions and outlets in interface file. First you have to Define actions and outlets in header file as shown in figure 1.4.1.

            Figure 1.4.1
 



            Then you have to connect actions and outlets as shown in figure 1.4.2. To connect Actions and outlets, right click on the files owner and drag to connect actions and outlets.
          






           
            Figure 1.4.2




           
           IBAction events include touch down, touch up inside , value changed etc. Developer can choose any touch event as per his requirement.


           
            1.5 Learning about Objects in Xcode

           
            Objects are listed in the right pane of Xcode project window. The set of objects that are present in the right pane include Label, Table View, Text Field, Button, etc. User can just drag and drop these object in the interface files  and connect appropriate actions and outlets in to it.

        Outlets for Objects are defined in header file as UITextField, UILabel, UIButton, etc. Figure 1.5.1 show the List of Objects that are available in Xcode. User can set Property for particular object in the right top pane ( Attribute window) and change the size using size inspector window. Attribute include color, font , alpha, background color, etc.






           
            Figure 1.5.1






1.6 Sample Hello world Program using Xcode.

           
            To create a new iPhone project, choose File ➪ New Project. Figure 2-2 shows the different types of projects you can create using Xcode. The left panel shows the two primary categories — iPhone OS and Mac OS X. The iPhone uses the iPhone OS (which has since been renamed as iOS), so click the Application item listed under iPhone OS to view the different templates available for developing your iPhone application.
            Although there are quite a few types of iPhone applications you can create, for this chapter select the View-based Application template; and for the product, select iPhone (to target the iPad, select the iPad as the product). Click the Choose... button.
            The left panel of Xcode shows the groups in the project. You can expand each group or folder to reveal the files contained in it. The right panel of Xcode shows the files contained within the group or folder you have selected from the left panel. To edit a particular file, select it from the list, and the editor at the bottom of the right panel opens the file for editing. If you want a separate window for editing, simply double-click the file to edit it in a new window.



            Click on header file or double click on header file to open in new window. Declare Outlet for UILabel and Connect the UILabel  In Interface builder.  Set the text of Label in  Interface builder as “Hello World”. Save the Application using Command+s or Product->Save  and Build the application using Command+b or Product->Build . Finally Run the application in iPhone simulator using Command+r or  Product->run .
             Figure shows output for helloWorld Application





3 comments:

Jack said...

Nice Work Dude...........

Liffena said...

Super,,,,,,,

Unknown said...

Simply Good machi!!

Post a Comment

 
Loading