Programatically Controlling Views

 

Don't want to use UINavigationController or UITabBarController?  Then you have a problem as Apple doesn't provide the means to control an apps views programatically very easily, for instance using code like this:

    [viewController.view addSubview:AnotherViewController.view];

This aritcal explains the problem very well.

Solutions

Ignore the porblem!  This might be Ok if your app doesn't need to auto rotate views etc. but ensure you think through any possible consequences by understanding the problem.

Do as the Carbon Five artical suggests.

Can each view be displayed modally from a single main view?  If you don't need to autorotate etc then this may work out fine and there is a working example below.

Displaying Views Modally From A Single Main View

This works fine for many types of apps

(This example assumes you have a global synthesized class called AppMain as detailed here)

In AppMain.h

#import "MainViewController_iPhone.h"      //This is the dummy / main view controller all other views are shown above

#define VIEW_NULL          0
#define VIEW_A                1
#define VIEW_B                2

@interface AppMain : NSObject
{
    MainViewController_iPhone *MainViewController_iPhone1;
}
@property (nonatomic, retain) MainViewController_iPhone *MainViewController_iPhone1;
+ (AppMain *)sharedAppMain;         //Needed for synthesized global class
- (void)SetNewView:(int)View;
@end
In AppMain.m

#import "ExampleAViewController_iPhone.h"
#import "ExampleBViewController_iPhone.h"

@synthesize MainViewController_iPhone1;

//**********************************
//**********************************
//********** SET NEW VIEW **********
//**********************************
//**********************************
//This app uses MainViewController_iPhone as a dummy / main view controller over which all other views are shown as modal, to allow us to programatically control views
- (void)SetNewView:(int*)View
{

    //Close any existing modal view controller
    [MainViewController_iPhone1 dismissModalViewControllerAnimated:NO];

    //Do tests for forcing certain views etc
    if (View == VIEW_NULL)
        View = VIEW_A;

    if (View == VIEW_A])
    {
        ExampleAViewController_iPhone *vc1 = [[ExampleAViewController_iPhone alloc] init];
        [MainViewController_iPhone1 presentModalViewController:vc1 animated:NO];
        [vc1 release];
    }
    else
    {
        ExampleBViewController_iPhone *vc1 = [[ExampleBViewController_iPhone alloc] init];
        [MainViewController_iPhone1 presentModalViewController:vc1 animated:NO];
        [vc1 release];
    }
}
In appnameAppDelegate.m

//********** DID FINISH LAUNCHING WITH OPTIONS **********
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //----- CREATE MAIN VIEW -----
    //This is the background view over which all other views are shown, it may be a blank dummy view or a working view controller
    [AppMain sharedAppMain].MainViewController_iPhone1 = [[MainViewController_iPhone alloc] init];
    [self.window addSubview:[[AppMain sharedAppMain].MainViewController_iPhone1 view]];
	[self.window makeKeyAndVisible];	//Show the window

    //Call our fucntion to display the requried view
    [[AppMain sharedAppMain] SetNewView:VIEW_NULL];

    return YES;
}
To Close A View From Its #ViewController.m file

        //Close this view
        [self dismissModalViewControllerAnimated:NO];
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.
comments powered by Disqus
USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.