如何使用多个iOS自定义视图控制器而不使用导航控制器 [英] How to use multiple iOS custom view controllers without a navigation controller

查看:99
本文介绍了如何使用多个iOS自定义视图控制器而不使用导航控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个使用多种类型屏幕的应用程序 - 所有这些都保证了自己的自定义视图控制器。通过在我的方法中重新分配主窗口 rootViewController ,我成功地在视图控制器及其相关视图之间切换app委托如下:

I am building an app that uses multiple types of screens--all which warrant their own custom view controllers. I am successfully switching between view controllers and their related views by reassigning the main window's rootViewController with a method in my app delegate like the following:

- (void)changeRootViewController:(NSString *)controllerName
{
    if (controllerName == @"book") {
        rootViewController = (UIViewController *)[[BookViewController alloc] init];
        [self.window setRootViewController:rootViewController];
    } else if (controllerName == @"something_else") {
        // Use a different VC as roowViewController
    }
}

然而,我这样做的方式似乎不是最好的做法。我不想使用 UINavigationController UITabBarController 作为 rootViewController ,要么。这是否是错误的做法,如果是这样,我应该如何以不同的方式接近?

The way that I am doing this seems like it just can't be best practice, however. I don't want to use a UINavigationController or a UITabBarController as the rootViewController, either. Is this the wrong way to be doing this, and if so, how should I be approaching this differently?

我认为这会被覆盖在某处,但是(我觉得好像)我用Google搜索了它,寻找相关问题等等。对不起,如果我错过了什么!

I thought this would have been covered somewhere, but (I feel as if) I've Googled the heck out of it, looked for related questions, etc. Sorry if I've missed something!

推荐答案

这不是一个糟糕的解决方案。您基本上将一个视图设置为根视图。当你需要另一个UIViewController时,你设置另一个。请小心泄漏......

Its not a bad solution. You basically set one view as the root view. When you need another UIViewController you set another one. Just be careful for the leaks...


  • 使用retain创建rootViewController作为类的属性。

  • 在此之前:


rootViewController =(UIViewController *)[[BookViewController alloc]
init];

rootViewController = (UIViewController *)[[BookViewController alloc] init];

添加:

if(rootViewController){
    self.rootViewController=nil;
}

}

所以你发布了前一个。

So you release the previous one.

编辑1:有一件事:我的解释是基于你不想使用UINavigationController的事实。

Edit 1: One thing: my explanation here is based on the fact that you don't want to use an UINavigationController.

这篇关于如何使用多个iOS自定义视图控制器而不使用导航控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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