如何在没有导航控制器的情况下使用多个 iOS 自定义视图控制器

发布于 2024-11-25 19:19:38 字数 777 浏览 2 评论 0原文

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

- (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。这是这样做的错误方法吗?如果是,我应该如何以不同的方式处理这个问题?

我以为这会在某个地方被覆盖,但是(我感觉好像)我已经用谷歌搜索了它,寻找相关问题等等。如果我错过了一些东西,抱歉!

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弥繁 2024-12-02 19:19:38

实现此目的的一个好方法是使用 iOS5+ 的 UIViewController 拥有子 UIViewController 的能力(称为视图控制器包含)。我确实很难弄清楚如何做到这一点,直到我观看了 WWDC 视频,该视频详细解释了这一点。

简而言之,它允许您创建自己的父视图控制器,该父视图控制器拥有一系列子视图控制器。这个单父视图控制器可以(并且可能应该,除非你正在做一些非常奇特的东西:P)作为应用程序窗口的根视图控制器。这种让单个视图控制器充当父视图控制器(并促进子视图控制器的添加、删除和转换)的方法让人想起 UINavigationController 所做的事情(这是 Apple 的意图)。现在您可以创建自己的类似 UINavigationController 的父视图控制器,但具有完全不同的过渡动画和 UI。

例如,在父视图控制器中的 viewDidLoad 中,我添加第一个子控制器,如下所示:

self.currentlyDisplayedChildViewController = [[TheFirstViewController alloc] init];
[self addChildViewController:self.currentlyDisplayedChildViewController];
[self.view addSubview:self.currentlyDisplayedChildViewController.view];
[self.currentlyDisplayedChildViewController didMoveToParentViewController:self];

然后我将有一个函数来转换到下一个子视图控制器(注意:该函数属于父视图控制器 - 将充当您的 UINavigationController 的视图控制器)

- (void)transitionToViewController:(UIViewController *)nextChildViewController
{
    [self addChildViewController:nextChildViewController];
    __weak TheParentViewController *me = self;
    [self transitionFromViewController:self.currentlyDisplayedChildViewController
                      toViewController:nextChildViewController
                              duration:1.0f
                               options:UIViewAnimationOptionTransitionFlipFromLeft
                            animations:nil
                            completion:^(BOOL finished)
                            {
                                [nextChildViewController didMoveToParentViewController:self];
                                [me.currentlyDisplayedChildViewController willMoveToParentViewController:nil];
                                [me.currentlyDisplayedChildViewController removeFromParentViewController];
                                me.currentlyDisplayedChildViewController = nextChildViewController;
                            }];
}

真正好的一件事是您可以使用所有标准 UIViewAnimationTransition 选项(或在动画块中定义您自己的自定义动画。此外,任何方向轮换事件是自动从父视图控制器转发到子视图控制器。这是您自己进行自定义根视图控制器操作时最棘手的问题之一,

我建议您观看标题为“实现 UIViewController Containment”的 WWDC2011 视频。

One great way to do this is by using iOS5+'s UIViewController's ability to have child UIViewControllers (it's called view controller containment). I certainly had a difficult time figuring out how to do this until I watched the WWDC video that explains this in good detail.

In a nutshell, it allows you to create your own parent view controller that owns a series of child view controllers. This single parent view controller can (and probably should, unless you're doing some really fancy stuff :P) sit as your app's window's root view controller. This method of having a single view controller act as a parent (and facilitate the adding, removing, and transitioning of the child view controllers) is reminiscent of what UINavigationController does (which is Apple's intent). Now you can create your own UINavigationController-like parent view controller, but have totally different transition animations and UI.

As an example, in the parent view controller, in the viewDidLoad, I add the first child controller like this:

self.currentlyDisplayedChildViewController = [[TheFirstViewController alloc] init];
[self addChildViewController:self.currentlyDisplayedChildViewController];
[self.view addSubview:self.currentlyDisplayedChildViewController.view];
[self.currentlyDisplayedChildViewController didMoveToParentViewController:self];

Then I would have a function to do the transition to the next child view controller (NOTE: this function belongs in the parent view controller -- the view controller that's going to act as your UINavigationController):

- (void)transitionToViewController:(UIViewController *)nextChildViewController
{
    [self addChildViewController:nextChildViewController];
    __weak TheParentViewController *me = self;
    [self transitionFromViewController:self.currentlyDisplayedChildViewController
                      toViewController:nextChildViewController
                              duration:1.0f
                               options:UIViewAnimationOptionTransitionFlipFromLeft
                            animations:nil
                            completion:^(BOOL finished)
                            {
                                [nextChildViewController didMoveToParentViewController:self];
                                [me.currentlyDisplayedChildViewController willMoveToParentViewController:nil];
                                [me.currentlyDisplayedChildViewController removeFromParentViewController];
                                me.currentlyDisplayedChildViewController = nextChildViewController;
                            }];
}

One thing really nice is you can use all the standard UIViewAnimationTransition options (or define your own custom animation in the animations block. Additionally, any orientation rotations events are automatically forwarded from the parent view controller to the child view controllers. This was one of that hairiest problems with doing custom root view controller manipulations on your own.

I would suggest taking a look at WWDC2011 video titled "Implementing UIViewController Containment".

零時差 2024-12-02 19:19:38

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

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

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

添加以下内容:

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

}

因此您释放了前一个。

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

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

  • Create the rootViewController as property of the class with retain.
  • Before this:

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

Add this:

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

}

So you release the previous one.

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文