如何使用UISegmentedControl和UITabBarController优化视图控制器导航中的性能 [英] How to optimize performance in view controller navigation with UISegmentedControl and UITabBarController

查看:82
本文介绍了如何使用UISegmentedControl和UITabBarController优化视图控制器导航中的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在研究的项目中,设计决定是在顶部使用UISegmentControl,在底部使用UITabBarController。 UISegmentControl为3个不同的视图提供3个选择。当前,当选择该特定选项卡时,我的同事已将所有3个视图添加到NSArray,然后基于UISegmentControl,取消隐藏所选视图,另两个视图被隐藏。它似乎不符合Apple的延迟加载准则,而且似乎昂贵,因为3个viewDidLoads(对数据库进行查询)可以一次加载所有内容。第一次选择选项卡时,会因此而有些滞后,一次加载所有3个viewController。

On a project I'm working on, the design decision was to use a UISegmentControl at the top, with a UITabBarController on the bottom. The UISegmentControl has 3 choices for 3 different views. Currently, my coworker has added all 3 views to an NSArray when that particular tab is selected, and then based on the UISegmentControl, the view selected gets unhidden, and the other two are hidden. It seems to not follow Apple's guidelines of lazy loading and seems expensive since 3 viewDidLoads (where queries are made to a database) are getting all loaded at once. There is some lag because of it when the tab is selected for the first time, loading all 3 viewControllers at once.

是否有更好的方法?我看到了一个只有两个viewControllers的简单示例,以及一个可以在两个视图之间切换的按钮。这对我来说很有意义,因为您始终知道以前的视图是什么,并且可以从超级视图中删除该视图,展示新视图,释放旧视图。但是有3个选择,我不知道如何跟踪我的视图层次结构(因为我可能位于第0段,显示视图0,然后转到第2段,显示视图2)。我不确定如何检查显示的最后一个视图,即使那是最好的方法。我在想,如果有更好的选择来跟踪此情况,但仍然使用细分控件,那么在项目变得更复杂之前最好现在就做。谢谢!

Is there a better way to do this? I saw a simple example with just two viewControllers, and a button that would switch between the two views. That makes sense to me since you always know what your previous view was, and you can remove that view from the superview, present your new one, release your old one. But with 3 choices, I do not know how to keep track of my view hierarchy (since I could be on segment 0, showing view 0, and then go to segment 2, showing view 2). I am not sure how to check for the last view that was shown, and even if that's the best method. I'm thinking that if there is a better option to keep track of this, but still using the segment control, might as well do it now before the project gets more complex. Thanks!

推荐答案

我建议创建一个根视图控制器,其工作是管理段控制并加载适当的VC,具体取决于在分段控件中选择哪个按钮上。根VC的视图将具有一个SubView,在该视图中插入了分段控件的VC视图。像这样的东西:

I would suggest creating a root view controller whose job it is to manage the segment control and load the proper VC depending on which button in the segmented control is selected. The root VC's view would have a subView where the segmented control's VC views are inserted. Something like:

- (void)segmentAction:(id)sender
{
    NSParameterAssert([sender isKindOfClass: [UISegmentedControl class]]);
    switch ([sender selectedSegmentIndex]) {
        case 0:
            MYViewController1 *vc = [[MyViewController1 alloc] init];
            self.segmentVC = vc;
            self.segmentSubvew = vc.view;
            [vc release];
            break;
    }
}

人们倾向于挂断的一件事是,需要每屏满内容仅1 VC-虽然最初是Apple推荐的内容,但此后他们更改了此建议。因此,将段特定的VC加载到SegmentManagerVC中是完全可以接受的。

One thing people tend to get hung up on is that there needs to be only 1 VC per screenfull of content -- while that was originally what was recommended by Apple, they have since changed this recommendation. So, loading your segment specific VCs inside the SegmentManagerVC is perfectly acceptable.

您可以进一步调整此设计以提高性能。例如,您可以首先为默认选定的段加载VC,然后延迟加载其他两个段,以便在选择其他段时它们已经可用。但是,如果采用这种方法,请确保连接 -didReceiveMemoryWarning 来释放当前未查看的两个VC。

You could further tweak this design for performance. For example, you could initially load the VC for the default selected segment and then lazy load the other two so they are already available when a different segment is selected. If you take this approach, though, be sure to hook up -didReceiveMemoryWarning to release the two VCs that aren't currently being viewed.

这篇关于如何使用UISegmentedControl和UITabBarController优化视图控制器导航中的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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