如何在没有NavgationController的情况下打开UIViewControllers和keepstate

时间:2013-10-08 07:04:46

标签: ios uiview uiviewcontroller

我的申请中有3个观看次数。

我想知道在点击按钮时如何正确打开和加载视图。

目前,当从第一个视图点击按钮时,我打开第二个视图,如下所示

[self dismissViewControllerAnimated:NO completion:nil];

    getPLViewController = [[GetPLViewController alloc] initWithNibName:@"GetPLViewController" bundle:nil];

    UIWindow* keyWindow= [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:getProjectListViewController.view];

    [self presentViewController:getPLViewController animated:NO completion:nil];

现在第二个视图已打开,我打开第三个视图

currentPLViewController = [[CurrentPLViewController alloc] initWithNibName:@"CurrentPLViewController" bundle:nil];


UIWindow* keyWindow= [[UIApplication sharedApplication] keyWindow];
[keyWindow addSubview: currentPLViewController.view];

[self presentViewController:currentPLViewController animated:NO completion:nil];

我不知道这是否正确,因为如果我尝试在前一个视图加载完成之前加载它或者沿着这些行添加某些内容,那么我一直遇到问题就会出现问题。

这是我从视图控制器返回的方式

[self dismissViewControllerAnimated:NO completion:nil];

所以我想知道这是一种正确的做法吗?还是有更好的方法?

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

为什么不在根视图上使用UINavigationController?

您可以隐藏导航栏,它会更干净,并且会与Apple指南相匹配以推送视图。

假设您使用XIB。 当您从AppDelegate添加第一个视图时,添加UINavigationController并隐藏此视图:

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

在您的RootViewController(第一个视图)旁边添加一个按钮,然后按SecondViewController

- (IBAction)displaySecondView
{
    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
    [self.navigationController pushViewController:secondViewController animated:YES];
}

在你的SecondViewController推送第三个:

- (IBAction)displayThirdView
{
    ThirdViewController *thirdViewController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    [self.navigationController thirdViewController animated:YES];
}

以及回到上一个视图的操作:

- (IBAction)back
{
    [self.navigationController popViewControllerAnimated:YES];
}