如何在没有导航控制器的情况下使用多个 iOS 自定义视图控制器 [英] How to use multiple iOS custom view controllers without a navigation controller

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

问题描述

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

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
    }
}

然而,我这样做的方式似乎不是最佳实践.我也不想使用 UINavigationControllerUITabBarController 作为 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?

我以为这会在某处被覆盖,但是(我感觉好像)我已经用谷歌搜索了它,寻找相关问题等.如果我错过了什么,抱歉!

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...

  • 使用保留创建 rootViewController 作为类的属性.
  • 在此之前:

rootViewController = (UIViewController *)[[BookViewController alloc]初始化];

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

添加:

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

}

所以你释放了上一个.

编辑 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天全站免登陆