Monday, 19 December 2011

Storyboard


      Storyboarding is an new feature in iOS 5 that will save you a lot of time building user interfaces for your apps.
                       If you have an app with many different screens then storyboards can help reduce the amount of glue code you have to write to go from one screen to the next. Instead of using a separate nib file for each view controller, your app uses a single storyboard that contains the designs of all of these view controllers and the relationships between them.


This how storyboard looks

Open Xcode Create a new project Select template for Project then hit on next, check Use storyboard.
hit on next.





         Our new project consists of two classes, AppDelegate and ViewController, and the MainStoryboard.storyboard file. Notice that there are no .xib files in the project, not even MainWindow.xib.The Storyboard Editor looks and works very much like Interface Builder. You can drag new controls from the Object Library (see bottom-right corner) into your view controller to design its layout. The difference is that the storyboard doesn’t contain just one view controller from your app, but all of them.


Click on AppDelegate.h and you’ll see it looks like this:
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window; 
@end  
If you look into AppDelegate.m, you’ll see that it does absolutely nothing, all the methods are practically empty. Even application:didFinishLaunchingWithOptions: simply returns YES. Previously, this would either add the main view controller’s view to the window or set the window’s rootViewController property, but none of that happens here.

So how does the storyboard get loaded by the app if there is no MainWindow.xib file?

In nib-, or “Main nib file base name”, that instructed UIApplication to load MainWindow.xib and hook it into the app. Our Info.plist no longer has that setting.

Instead, storyboard apps use the key UIMainStoryboardFile, or “Main storyboard file base name”, to specify the name of the storyboard that must be loaded when the app starts. When this setting is present, UIApplication will load the MainStoryboard.storyboard file and automatically instantiates the first view controller from that storyboard and puts its view into a new UIWindow object. No programming necessary.







This is how Xcode will look after creating single view based application. the arrow indicates initial view controller.
For Pushing or Presenting view controller no need of writing the code , just by Control dragging from on view controller to other then select the Push or Modal.
Drag one more View controller and  place one button  in the first view controller from button  ctrl drag to the second view controller.


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
         -The prepareForSegue method is invoked whenever a segue is about to take place.
          The main use of this method is when view controller about to push or present you can
          pass the necessary data to view controller which is about to come.  
      
        
         - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
                 if ([segue.identifier isEqualToString:@"tableSampleSegue"]) {
                    tableSample *theSampleViewCntrl=segue.destinationViewController;
                    NSArray *theArray=[NSArray arrayWithObjects:@"1",@"2",@"3",@"4"nil];
             theSampleViewCntrl.mTableData=theArray
               }
        }

   



    In the above example passing the theArray to tableSample TableViewController.

   The code from prepareForSegue checks for that identifier (“ tableSampleSegue”). It is recommended to always do such a check because you may have multiple outgoing segues from one view controller and you’ll need to be able to distinguish between them .











  

No comments:

Post a Comment