如何在没有 NavgationController 的情况下打开 UIViewControllers 和 keepstate [英] How to open UIViewControllers and keepstate without NavgationController

查看:33
本文介绍了如何在没有 NavgationController 的情况下打开 UIViewControllers 和 keepstate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有 3 个视图.

I have 3 views in my application.

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

I would like to know how to properly open and load views when buttons are clicked.

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

Currently when a button is clicked from the first view I open the second view like this

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

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

And now that the second view is open I open the third like this

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


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

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

我不知道这是否正确,因为如果我在前一个视图完成加载之前尝试加载它或类似的东西,我会遇到显示在其侧面的视图的问题.

I have no idea if this is correct anymore as I have been having trouble with the view showing up on its side if I try to load it before the previous view has finished loading or something along those lines.

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

This is how I go back from a viewcontroller

[self dismissViewControllerAnimated:NO completion:nil];

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

So I would like to know is this a correct way of doing it? or is there a better way?

任何帮助将不胜感激.

推荐答案

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

Why you don't use a UINavigationController on your root view ?

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

You can hide the navigation bar and it will be cleaner and it will match with the Apple guidelines to push views.

假设您使用 XIB.当你从 AppDelegate 添加你的第一个视图时,添加一个 UINavigationController 并隐藏这个:

Supposing you use XIB. When you add your first view from the AppDelegate, add an UINavigationController and hide this one :

- (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 :

Next on your RootViewController (first view) you add a button and push the SecondViewController :

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

同样在你的 SecondViewController 中推送第三个:

The same in your SecondViewController to push the third one :

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

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

and an action to back to the previous view :

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

这篇关于如何在没有 NavgationController 的情况下打开 UIViewControllers 和 keepstate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆