Tuesday, 29 May 2012

Storyboards

Introduction
We are all used to using view controllers now. We know how to use navigation controllers in order to push and pop view controllers. Apple believes this can be done easier and that's the whole story behind storyboards. Story boarding is the new way of defining the connection between different screens in your app. For instance, if you have 20 unique view controllers in your app which you coded a year ago, looking at the source code now you will need to find your way around the connection between these view controllers and what view controller is pushed when a certain action is taken by the user. This can be very difficult especially if you have not documented your code. Story- boards to the rescue. With storyboards, you can now view/create your entire app's UI and the connection between each view controller in one screen. It's that simple.

With storyboards, one screen's worth of content is called a scene. A scene on the iPhone can be thought of as the view of a view controller, where you put all your contents on the screen to be presented to the user at the same time. On the iPad, more than one scene can be presented to the user at the same time because of the bigger screen. Story- boarding supports transitioning from one scene to another, similar to a navigation controller pushing a view controller on top of another. That is a transition. Another type of transition is a modal view controller that slides from the bottom of the screen up to fill the screen temporarily. On the iPad, modal screens appear in the center of the screen and dim the rest of the screen usually to point out that they are the main input at that moment.

 Creating a Project with Storyboards:
  1.  In Xcode, select the File meny and then New→New Project...
  2. .In the New Project dialog, make sure iOS main category is selected and select Application sub-category under iOS. Once that is done, on the right side, select Single View Application and press Next
  3. Now select a product name and make sure your app is a Universal app.Apple wants developers to start writing universal apps whenever they can in order for iPad users to also enjoy the same apps that iPhone and iPod Touch users can enjoy. In this dialog, make sure you have checked the User Storyboards check box.
  4. You'll notice two files whose names end with .storyboard. Xcode, because this is a universal app, created a storyboard for iPhone and another storyboard for iPad
Adding a Navigation Controller to a Storyboard

Note:You want to be able to manage multiple view controllers inside a storyboard-based application.

In order to add a navigation controller to your storyboard-based app, simply follow these steps:
  1. Click on the iPhone story board that Xcode created for you.I have name demoproject Adding a Navigation Bar to a Storyboard so my iPhone storyboard file is Main- Storyboard_iPhone.storyboard. Once you click on this file, Interface Builder will display the contents of this file automatically.
  2. Once the storyboard file is open in IB (Interface Builder), simply double-click on an empty space on the storyboard's canvas and you will see the content shrink in size and give you more free space.
  3. Under the View menu, select Utilities→Show Object Library.
  4.  In the Object Library, find the Navigation Controller object and drag and drop it into the storyboard, to the left side of your existing view controller.
  5. As you can see, the navigation controller now has added another view controller to our UI. What we need to do is to simply delete this view controller by selecting it first and then pressing the Delete button on the keyboard. Now we are left with the navigation controller and our original view controller(Delete Root View).
  6. Now click once on the navigation controller object on the storyboard. Once the navigation controller object is selected, hold down the Control key on your keyboard and the left button on your mouse and drag your mouse over to the view controller (on the right) that we had on our storyboard originally. This will draw a line from the navigation controller all the way to the view controller
  7. Now release your mouse button at which point you will be presented with a dialog asking you what type of connection you want to create between the navigation and the view controller. Select the root View Controller item from the list by simply clicking on it.
  8. After this is done, the storyboard will show that your navigation controller is now connected to the original view controller.
  9. The most important step is to make our navigation controller the initial/root view controller. If we don't do this, the storyboard will use the view controller that we initially had as the initial view controller. Can you see that the view controller on the right side has a border around it. That indicates an initial view controller. To make our navigation the initial view controller, simply select the Navigation Controller under the Navigation Controller Scene panel in Interface Builder,  Now select the View menu in Xcode and choose View→Show Attributes Inspector. Once the attributes inspector is opened, under the View Controller category, check the Is Initial View Controller check box.
As you can see, now your navigation controller has a border around it. Now if you run your application, you will notice that the initial view controller has a navigation bar on top, indicating that this view controller now has a navigation controller. In the next recipes we will see how we can make use of the navigation controller to display new scenes on the screen.

We now have a navigation controller with a view controller inside it but our objective now is to trigger an action and then move from one view controller to another. Alright then; let's place a button on our view controller and push a view controller into the stack once the user presses the button.  Alright....
  1. Go back to your .storyboard file.
  2. In the Object Library, find the View Controller object and drag and drop it onto the storyboard, on the right side of our existing view controller.
  3. In Object Library, find the Button object and drag and drop it into the first view controller. Note that if you are zoomed out, Interface Builder will not allow you to drop a button onto a view controller. You need to double click on an empty space on your storyboard to zoom into it before Interface Builder allows you to drop UI components onto your view controllers.
  4. Now select the button,hold down the Control key on your key board and then hold down the left mouse button and on the button and drag all the way to the second view controller
  5. Now lift your fingers off the mouse button and the Control key on your keyboard. You will now be presented with a dialog . Simply click on the performSegueWithIdentifier:sender: item Now if you have a look at your storyboard, you will see that the first view controller is connected to the second view controller.
  6. Now if you run your app and tap on the button on the first view controller, you'll see that the second view controller will automatically get pushed onto the stack of view controllers. Once the second view controller is presented, you will see a back button on the navigation bar. If you press that button, you will be sent back to the first view controller.

Wednesday, 16 May 2012

Design Patterns and View Controllers

          Types of Design Patterns
    • Singleton
    • Delegation
    • Model View Controller       
           Singleton
           The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. 
           Ensure a class has only one instance, and provide a global point of access to it.
           Delegation
           A delegate is a type that references a method. Once a delegate is assigned a method,it behaves exactly like that method. The delegate method can be used like any other method, with parameters and a return value.Delegates are declared using the keyword delegate.
    Note:Once instantiated, the delegate reference can be used directly as a method
           Model View Controller
           Model View Controller: In the Model-View-Controller (MVC) design pattern, a controller object provides the custom logic needed to bridge the application’s data to the views and other visual entities used to present that data to the use.  


      
  • View controller should encapsulate all of the behavior associated with managing the views in its view hierarchy.
  • Each view controller is responsible for managing a discrete part of your application’s user interface.
  • The view controller  acts  as the central coordinating agent for the view hierarchy, handling exchanges between its views and any relevant controller or data objects.
  • A view controller is a specific type of controller object that you use to present and manage a set of views.
    • View controller objects are descendants of the UI View Controller class, which is defined in the UIKit framework.
    • A view controllers provide a vital link between an application’s data and its visual view. 

      View Controller Class

    • View controller classes available in the UIKit framework along with some of the key classes used in conjunction with view controllers.
    • These classes are often used internally by view controller objects to implement special types of interface 

              Types of View Controller

            1. Custom View Controller
            2. Table View Controller
            3. Container View Controllers
                      i. Navigation Controller
                      ii.Tab Bar Controller
                      iii.Split View Controller
            4.Window based View Controller
           
          Custom View Controller
           Each custom view controller object you create is responsible for managing all      single view hierarchy.
           Table View Controller
           The UI Table View Controller  class is another  type of custom view controller designed specifically for managing  tabular  data.The class adds automatic support for many standard table-related behaviors such as selection management, row editing, table configuration, and others.
           Navigation Controller
           The navigation template provide a starting point for an application that uses a navigation controller .It provide a user interface configuration with navigation controller to display a list of item.Navigation controller’s primary job is to act as a manager of other view controllers.It also manages a few views. Specifically, it manages a navigation  bar that displays information about the user’s current location in the data hierarchy, a back button for navigating to previous screens, and any custom controls the current view controller  needs.The navigation controller also manages an optional toolbar, which can be used to display commands related to the current screen.
           Tab Bar Controller
           A tab bar controller is a container view controller that you use to divide your application into two or more distinct modes of operation  
           Split View Controller 
              Split template provide a starting point for an application that uses a split view controller.It provide user interface configuration with a split view controller and two view controller to manage a master detail style view.A split-view controller is a container  view controller that is typically used to implement master-detail interfaces Split-view controllers are supported on iPad only.
          Window based View Controller
           Window based view controller provide application delegate and window.

     

Getting Started iPhone Development



The iPhone changed everything.
The iPhone 4 “changed everything, again.” And now you’ve got the iPad to contend with, too. iOS devices are now capable word processors, e-readers, and video cameras. They are being used in business and medicine as enterprise devices, and the App Store is a platform for every developer to use, from one-man shows to big-name companies. Apple provides the software and we’ll help you with the knowledge—
let’s get started So, you want to build an iOS app...
Maybe you use an iPhone all the time and wish “it could do that.” Maybe you have an app you love that could be so much better. You might have a business that wants to leverage computing power that fits in your customers’ hands. Or perhaps you have an idea for an app that could start a business. There are a lot of motivations to want to code for iPhone and iPad, and lots of customers, too.
For iPhone development first you should be clear Programming Language like C,C++ ......and Object Oriented Programming language concepts or OOP(Object oriented Programming) pillars.
  1. Objects
  2. Class
  3. Inheritance
  4. Data Abstraction
  5. Data Encapsulation
  6. Polymorphism
  7. Dynamic Binding
  8. Message Passing
Note:Objective-C does not allow multiple inheritance. Therefore, every class is the direct descendant of, at most, one other class.

The root class of most Objective-C objects is the NSObject class. This class manages the runtime capabilities offered by iOS; as a result, any class that directly or indirectly inherits from NSObject will inherit these capabilities as well. As we will see later in this chapter, objects that inherit from NSObject can take advantage of Objective-C’s dis- tinctive memory management model.