HurkNburkS
HurkNburkS

Reputation: 5510

How to load and unload a UIViewController from the window

I have create some logic when the app is loaded that I can load from 3 different views depending on some values I set in my plist.

This is what my code looks like

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    //sets context for coredata
    CoreDataController *coreDataController = [CoreDataController sharedManager];
    coreDataController.managedObjectContext = self.managedObjectContext;

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    PrefsController *prefsController = [[PrefsController alloc] init];
    NSDictionary *prefsDictionary = [prefsController readPrefs];

    NSLog(@"%@", prefsDictionary);

    NSString *projectListBoolString = [prefsDictionary objectForKey:@"ProjectListAvailable"];
    NSString *installsBoolString = [prefsDictionary objectForKey:@"InstallsAvailable"];
    NSString *finishinBoolString = [prefsDictionary objectForKey:@"FinishingAvailable"];


    if (([projectListBoolString isEqualToString:@"T"]) && ([installsBoolString isEqualToString:@"F"]) && ([finishinBoolString isEqualToString:@"F"])) {
        self.getProjectListViewController = [[GetProjectListViewController alloc] initWithNibName:@"GetProjectListViewController" bundle:nil];
        self.window.rootViewController = self.getProjectListViewController;
        [self.window makeKeyAndVisible];
    }
    else if (([projectListBoolString isEqualToString:@"T"]) && ([installsBoolString isEqualToString:@"T"]) && ([finishinBoolString isEqualToString:@"T"])) {
        self.currentProjectListViewController = [[CurrentProjectListViewController alloc] initWithNibName:@"CurrentProjectListViewController" bundle:nil];
        self.window.rootViewController = self.currentProjectListViewController;
        [self.window makeKeyAndVisible];
    }
    else {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        self.window.rootViewController = self.viewController;
        [self.window makeKeyAndVisible];
    }

    return YES;
}

I would like to be able to then load and unload UIViewControllers (including removing from memory by using buttons presses etc.

I dont want to use a navigaiton based controller as I want the views to be static or individual if that makes more sense.

If someone could show me some example code to load a new UIViewController to the window and remove the old UIViewController that would be greatly apprecaited.

However I am not sure of the correct was to handle this, or even how the code looks.

any help would be greatly appreciated.

Upvotes: 0

Views: 822

Answers (1)

Jordan Montel
Jordan Montel

Reputation: 8247

For me, is not a good way to load your view like this. It would be better to load a rootViewController in your AppDelegate and add your current view depending of your plist inside the RootViewController :

AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
    self.window.rootViewController = rootViewController;
    [self.window makeKeyAndVisible];
    return YES;
}

After in your RootViewController add a subview of the current view depending on your plist :

RootViewController.m

#pragma mark - View management
- (void)viewDidLoad
{
    [super viewDidLoad];

    NSDictionary *prefsDictionary = [prefsController readPrefs];

    NSLog(@"%@", prefsDictionary);

    NSString *projectListBoolString = [prefsDictionary objectForKey:@"ProjectListAvailable"];
    NSString *installsBoolString = [prefsDictionary objectForKey:@"InstallsAvailable"];
    NSString *finishinBoolString = [prefsDictionary objectForKey:@"FinishingAvailable"];

    if (([projectListBoolString isEqualToString:@"T"]) && ([installsBoolString isEqualToString:@"F"]) && ([finishinBoolString isEqualToString:@"F"])) {
        self.getProjectListViewController = [[GetProjectListViewController alloc] initWithNibName:@"GetProjectListViewController" bundle:nil];
        // Add View Controller
        [self.view addSubview:self.getProjectListViewController.view
    }
    else if (([projectListBoolString isEqualToString:@"T"]) && ([installsBoolString isEqualToString:@"T"]) && ([finishinBoolString isEqualToString:@"T"])) {
        self.currentProjectListViewController = [[CurrentProjectListViewController alloc] initWithNibName:@"CurrentProjectListViewController" bundle:nil];
        // Add View Controller
        [self.view addSubview:self.currentProjectListViewController.view];
    }
    else {
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        // Add View Controller
        [self.view addSubview:self.viewController.view];
    }
}

Upvotes: 1

Related Questions