ios - 如何在没有NavgationController的情况下打开UIViewControllers并保持状态

标签 ios uiview uiviewcontroller

我的应用程序中有 3 个 View 。

我想知道如何在单击按钮时正确打开和加载 View 。

当前,当从第一个 View 中单击按钮时,我会像这样打开第二个 View

[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];

现在第二个 View 打开了,我像这样打开第三个 View

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


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

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

我不知道这是否正确,因为如果我尝试在前一个 View 完成加载之前加载它,或者类似的事情,我一直遇到 View 显示在其一侧的问题。

这就是我从 View Controller 返回的方式

[self dismissViewControllerAnimated:NO completion:nil];

所以我想知道这样做是否正确?或者有更好的方法吗?

如有任何帮助,我们将不胜感激。

最佳答案

为什么不在 Root View 上使用 UINavigationController?

您可以隐藏导航栏,它会更干净,并且符合 Apple 的推送 View 指南。

假设您使用 XIB。 当您从 AppDelegate 添加第一个 View 时,添加一个 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(第一个 View )上添加一个按钮并按下 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];
}

以及返回到上一个 View 的操作:

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

关于ios - 如何在没有NavgationController的情况下打开UIViewControllers并保持状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19241205/

相关文章:

ios - 如何在使用它的 ViewController 上调整自定义(在单独的文件中创建)UIView 高度?

ios - Swift - 使用 NSUserDefaults 保存高分

ios - 使用核心数据从 TableView 打开编辑 View Controller

ios - 如何从 SWIFT 3 中的另一个 ViewController 检查 UISwitch 的状态?

html - 为什么这个网站在较小的设备上不是完整的视口(viewport)宽度?

ios - UIView 动画 - 奇怪的行为(Swift)

ios - 自动调整掩码将 UIView 附加到屏幕底部而不是 UIScrollView

ios - 设置整页广告

ios - 主从应用程序背景

ios - 快速将 GPS 元数据字典添加到使用 AVFoundation 拍摄的图像