Labels

Learn the powerful enterprise adaptable database:

Getting Started With ADABAS & Natural

Saturday, March 9, 2013

Xcode 4.6: RootViewController

This tutorial guides the reader to manually create an iOS application that allows the user to switch between two views. Here, a RootView Controller is used to control ChildView Controllers. Manual works help the programmer to understand more on how the Model-View-Control concept is implemented in Xcode 4.6




A) CREATE PROJECT
1) Create a New Project
2) Type: Empty Application
3) Name: MasterSwitcher
4) Device Family: iPhone
5) Use Core Data... : No
6) Include Unit Test... : No
7) Automatic Reference Counting : Yes



B) CREATE MASTER SWITCH CONTROLLER
1) Create a New File.
2) Category: Cocoa Touch
3) Class Name: MasterSwitchController
4) Sub Of Class: UIViewController
5) Targeted for iPad: No
6) XIB for user interface: No



C) CREATE CHILD VIEW CONTROLLER
(Repeat the steps above)
Class Name: ChildViewController1 and ChildViewController2



D) CREATE MASTER VIEW (XIB)
1) Create a New File.
2) Category iOS/User Interface.
3) Type: View.
4) Device Family: iPhone.
5) View Name: MasterView



E) CREATE CHILD VIEWS
(Repeat the steps above)
View Name: ChildView1 and ChildView2



F) MODIFY  APP DELEGATE

1) Edit AppDelegate.h

#import <UIKit/UIKit.h>
@class MasterSwitchController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) MasterSwitchController   *masterSwitchController;
@end




2) Edit AppDelegate.m

#import "AppDelegate.h"
#import "MasterViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application
       didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

// Override point for customization after application launch
self.switchViewController = [[MasterViewController alloc] initWithNibName:@"MasterView" bundle:nil];
   UIView *masterView = self.masterViewController.view;
   CGRect masterViewFrame = masterView.frame;
   masterViewFrame.origin.y += [UIApplication sharedApplication].statusBarFrame.size.height;

   masterView.frame = masterViewFrame;
    self.window.rootViewController = self.masterViewController;
   self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

return YES; }
...
@end




G) MODIFY MASTER CONTROLLER HEADER
1) Edit MasterViewController.h:

#import <UIKit/UIKit.h>
@class ChildViewController1;
@class ChildViewController2;

@interface MasterViewController : UIViewController
@property (strong, nonatomic) ChildViewController1 *childViewController1;
@property (strong, nonatomic) ChildViewController2  *childViewController1;

- (IBAction)switchViews:(id)sender;
@end



H) MODIFY MASTER VIEW XIB

1) Select MasterView.xib.
2) Click File's Owner icon, go to Identity Inspector. Change the Class From NSObject to MasterSwitchController. (Binding View and Controller).
3) Add Toolbar to the bottom of View. Rename the label as Switch.
4) Select toolbar button, control-drag to File's Owner icon and link to switchViews:action.
5) Control-drag from File's Owner icon to the View icon and select Views:outlet.



I) MODIFY MASTER CONTROLLER IMPLEMENTATION
File: MasterSwitchController.m


1) Edit imports section:
#import "MainViewController.h"
#import "ChildViewController1.h"
#import "ChildViewController2.h"

2) Edit viewDidLoad section:

- (void)viewDidLoad
{

[super viewDidLoad];
// Do any additional setup after loading the view. self.childViewController1 = [[ChildViewController1 alloc] initWithNibName:@"ChildView" bundle:nil];
   [self.view insertSubview:self.childViewController1.view atIndex:0];

}


3) Edit switchViews section:
- (IBAction)switchViews:(id)sender {
   if (self.childViewController2.view.superview == nil) {

      if (self.childViewController2 == nil) {
           self.childViewController2 =
           [[ChildViewController2 alloc] initWithNibName:@"ChildView2"     bundle:nil];

      }
       [self.childViewController1.view removeFromSuperview];
       [self.view insertSubview:self.childViewController2.view atIndex:0];

} else {
     if (self.childViewController1 == nil) {
        self.childViewController1 =
         [[ChildViewController1 alloc] initWithNibName:@"ChildView1"    bundle:nil];
    }
     [self.childViewController1.view removeFromSuperview];
     [self.view insertSubview:self.childViewController1.view atIndex:0];
} }






4) Edit didReceiveMemoryWarning section:
- (void)didReceiveMemoryWarning {
   // Releases the view if it doesn't have a superview
   [super didReceiveMemoryWarning];
  // Release any cached data, images, etc, that aren't in use
  if (self.childViewController1.view.superview == nil) {
       self.childViewController1 = nil;
  } else {
      self.childViewController2 = nil;
}

}



Download Sample

No comments:

Post a Comment