How to use Multiview Controller in iphone?

It’s most important to understand that each of these multiview controller is a view controller. Even the provided multiview classes UITabBarController and UINavigationController are subclasses if UIViewController and can do anything other view controller can do.

Step 1: First select New Project from File menu. When the assistant opens up, select Window-Based Application, and type in a project name of  View Switcher. The template we just selected is actually even simpler than the one  we’ve been using up to now. this template will give us a window, an application delegate, and nothing else no views, no controllers, no nothing.

Step 2: We are going to create all the files that will make up our application before we do anything in Interface Builder and before we write any code. Single click the classes folder in the Groups & Files pane, and then select New File… from the File menu. You select Cocoa Touch Classes  and then select UIViewController subclass, and click Next. Type in the name SwitchViewController.m and make sure that Also create “SwitchViewController.h”. Repeat the same steps two more times to create BlueViewController.m and YellowViewController.m. We also create two more nib files, single click the Resource folder in the Groups & Files , and then select New File from the File menu select User Interface under the iphone OS. Select the icon for the View xib template, which will create a nib with content view and save it BlueView.xib. Repeat the steps to create a second nib file called YellowView.xib.

Step 3: Our first stop on the multiview express is the application delegate.  The IBOutlet declaration you just typed is an outlet that will point to our application’s root controller. We need this outlet because we are about to write code that will add the root controller view to our application’s main window. Single click the file View_SwitcherAppDelegate.h.


#import <uikit/UIKit.h>
    @class SwitchViewController;

     @interface View_SwitcherAppDelegate : NSObject {
     UIWindow *window;
     IBOutlet SwitchViewController *switchViewController;



Step 4: Save, your source code, and double click MainWindow.xib to open it in Interface Builder. Four icons should appear in the nib’s main  window. We need to add one more icon that will represent an instance of our root controller. Since Interface Builder’s library doesn’t have SwitchViewController, we’ll have to add a view controller and change its class to SwitchViewController. Drag A View from the library onto the window . Now grab a tootbar from the library, drag it onto your view, and place it at the bottom. Double click the botton, and change its title to Switch Views. Now you select the SwitchViews button , control-drag from it over to the SwitchViewController icon, and select the switchViews:action. Control drag from the View_SwitcherAppDelegate icon to the SwitchViewController icon, and select the switchViewController outlet. So now save your nib file, and go  back to the Xcode .

Step 5:  We override viewDidLoad to create an instance of BlueViewController in SwitchViewController.m file . We use the initWithNibName method to load the BlueViewController instance from the nib file BlueView.xib.


- (void)viewDidLoad {
    BlueViewController *blueController = [[BlueViewController      alloc]initWithNibName:@"BlueView" bundle:nil];
   self.blueViewController = blueController;
   [self.view insertSubview:blueController.view atIndex:0];
   [blueController release];
   [super viewDidLoad];



Step 6: We will load the yellow view the first time we actually need it. The yellow view happens in the switchViews: method, switchView: first checks to see if the property yellowViewController is nil.  So, we need to create an instance of yellowViewController  as we did for the BlueViewController in the viewDidLoad method.


    if(self.yellowViewController == nil)
     YellowViewController *yellowController=[[YellowViewController alloc]    initWithNibName:@"YellowView" bundle:nil];
     self.yellowViewController = yellowController;
    [yellowController release];
    [UIView beginAnimations:@"View Flip" context:nil];
    [UIView setAnimationDuration : 1.25];
    [UIView setAnimationCurve : UIViewAnimationCurveEaseInOut];

   if(self.blueViewController.view.superview == nil)
     [UIView setAnimationTransition:
     forView:self.view cache:YES];
     [blueViewController viewWillAppear:YES];
     [yellowViewController viewWillDisappear:YES];
     [yellowViewController.view removeFromSuperview];
     [self.view insertSubview:blueViewController.view atIndex:0];
     [yellowViewController viewDidDisappear:YES];
      [blueViewController viewDidAppear:YES];
     [UIView setAnimationTransition:
     forView:self.view cache:YES];
     [yellowViewController viewWillAppear:YES];
     [blueViewController viewWillDisappear:YES];
     [blueViewController.view removeFromSuperview];
     [self.view insertSubview:yellowViewController.view atIndex:0];
     [blueViewController viewDidDisappear:YES];

    [UIView commitAnimations];



Step 7:  Now double click BlueView.xib to open the file , and single click the File’s Owner icon bring up the identity inspector. Change NSObject to BlueViewController. Next bring up attributes inspector , change the background color of view to a shade of blue. Drag aRoundRect Button from the library over the window. then switch to the connection inspector,drag from the Touch Up inside event to the File’s Owner icon and connect to the blueButtonPressed:action method. Do it one more time for YellowView.xib file. Now save it, go back to Xcode.

Step 8: Compile your application and lunch it  on your simulator.

Leave a Comment:

1 comment
Ruslan says February 15, 2011

– (IBAction)switcherViews:(id)sender {
NewViewController *newController = [[NewViewController alloc] initWithNibName:@"NewView" bundle:nil];
self.newViewController = newController;
[self.view insertSubview:newController.view atIndex:0];
[newController release];

error: request for member ‘NewViewController’ in something not a structure or union

Need help!

Add Your Reply