0

我在加载应用程序时创建了一些逻辑,我可以根据我在 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(包括通过使用按钮等从内存中删除)。

我不想使用基于导航的控制器,因为我希望视图是静态的或单独的,如果这更有意义的话。

如果有人可以向我展示一些示例代码,以将新的 UIViewController 加载到窗口并删除旧的 UIViewController,这将非常受欢迎。

但是,我不确定正确的处理方法是什么,甚至代码的外观如何。

任何帮助将不胜感激。

4

1 回答 1

1

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

应用委托

- (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 添加当前视图的子视图之后:

根视图控制器.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];
    }
}
于 2013-10-09T09:40:09.970 回答