objective-c - 如何将 iPad View 分解为更易于管理的 block ?

标签 objective-c design-patterns ipad uiviewcontroller model-view-controller

我知道我可能会因为没有正确阅读 HIG 或一些文档而受到指责,但在观看了几个培训视频并在 iPhone 上构建了许多小项目之后,我现在正在尝试将东西组合在一起在 iPad 上,好​​吧,我的大脑受伤了。

我理解 iPhone 上的想法:一个 View = 一个屏幕,一个 View = 一个 View Controller 。但现在我正在处理一个可以同时显示多个内容的硬件,我真的不明白我应该如何使用它。

我给出的例子是我当前正在进行的项目...我已经为不同的内容屏幕(启动屏幕、菜单、文章 View 等)组合了一堆不同的 View Controller 。但随后一些屏幕共享了内容。例如,菜单和文章屏幕都有一个菜单栏(如工具栏),它从屏幕顶部向下滑动并提供导航。另外,我有一个导航元素,应该从屏幕底部向上滑动以导航到不同的文章。这些是我在许多其他 iPad 应用程序中看到的东西(《Wired》就是一个例子)。

但是我不应该一次在屏幕上放置多个 View Controller ,不是吗?不仅如此,如果我这样做,那么自动旋转功能就会开始变得有点疯狂。但是,如果我将所有内容放入单个屏幕的一个 View Controller 中,我最终会得到 1,000 行代码,其中大部分需要复制粘贴到其他 View Controller 中。对我来说这似乎是一个非常丑陋的解决方案。

所以我显然做错了什么......但我应该做什么?即使有人可以指出我应该阅读的信息的方向,我也会非常感激。

谢谢!

:-乔

最佳答案

您不被禁止一次显示多个 View ,其中 View Controller 只是驻留在内存中的 View 管理器。

您可以创建自定义 View Controller 来管理您的 View Controller 。将其视为 UINavigationController 或 UISplitViewController。

你有一个 NSMutableArray 的 viewControllers

@property (nonatomic, retain) NSMutableArray *viewController;

/*
 Do not use this code in production, its just for the example
*/
- (void)presentViewController:(UIViewController *)controller inRect:(CGRect)rect {

    // check if viewcontroller is valid object
    if (controller == nil) {
        return;
    }

    // retain the controller
    [self.viewController addObject:controller];

    // position controllers view in the desired rect
    // (in your implementation you might create a zones and use them instead)
    controller.view.frame = rect;

    // you might want to provide some animation capabilities here
    [self.view addSubview:controller.view];
}

现在,您可以根据需要在屏幕上拥有任意数量的 View 及其自己的 View Controller 。您可能有嵌套 View Controller ,例如: ViewControllerManager->NavigationController->[ViewControllers];

附注当然,完整的实现会更复杂,您需要调用 View Controller viewWill/viewDid,管理 View Controller View 动画(呈现和消除),lowMemoryWarning的内存管理(清除不可见或未使用的 View Controller )等等。

希望能有点帮助。 :)

关于objective-c - 如何将 iPad View 分解为更易于管理的 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3408558/

相关文章:

ios - 在 ObjC 中工作,而不是在 swift 中工作。我在 swift 中使用 JSON 解析有什么问题

objective-c - 从您的应用程序内部调用 map 获取方向 - iOS 5 iOS 6

oop - 什么时候可以依赖具体类?

iphone - 使用 iOS SDK 检查互联网连接

iphone - 我应该考虑什么来确保我的 iPhone 应用程序无缝移植到 iPad?

ios - 将复选框添加到 IOS 6 AlertView

ios - 使用 ios 5.1.1 时启动时的 SIGABRT

c++ - 来自 "Overload"杂志的奇怪收据

JavaScript 架构/应用程序结构最佳实践?

ios - 在 iPad 中显示矢量 map