如何从窗口加载和卸载UIViewController

时间:2013-10-09 09:26:35

标签: ios objective-c uiview uiviewcontroller

我在加载应用程序时创建了一些逻辑,我可以根据我在plist中设置的某些值从3个不同的视图加载。

这是我的代码看起来像

- (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;
}

我希望能够加载和卸载UIViewControllers(包括使用按钮等从内存中删除。

我不想使用基于navigaiton的控制器,因为我希望视图是静态的或个人的,如果这更有意义。

如果有人可以给我看一些示例代码来将新的UIViewController加载到窗口并删除那些非常适合的旧UIViewController。

但是我不确定处理这个问题是否正确,甚至代码看起来都不正确。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

对我来说,加载你的观点不是一个好方法。 最好在AppDelegate中加载rootViewController,并根据RootViewController中的plist添加当前视图:

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;
}

RootViewController之后,根据您的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];
    }
}