UISegmentedControl最佳做法 [英] UISegmentedControl Best Practice

查看:108
本文介绍了UISegmentedControl最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出将UISegmentedControl用于iPhone应用程序的最佳"方法.我在这里阅读了一些关于stackoverflow的帖子,并看到了一些人的想法,但是我还没有完全弄清实现这一点的最佳方法.我指的是:

I'm trying to work out the "best" way to use a UISegmentedControl for an iPhone application. I've read a few posts here on stackoverflow and seen a few people's ideas, but I can't quite sort out the best way to do this. The posts I'm referring to are:

从UISegmentedControl更改视图如何使用UISegmentedControl来切换视图?

似乎这些选项是:

  • 在IB中添加每个视图,然后将它们彼此重叠,然后显示/隐藏它们
  • 在IB中分别创建每个子视图,然后在主视图中创建一个容器,以填充所需的子视图
  • 设置一个非常高或非常宽的UIView,并根据选定的细分向左/向右或向上/向下为其设置动画
  • 使用UITabBarController换出子视图-看起来很愚蠢
  • 对于表,请重新加载表并在cellForRowAtIndex中,然后根据所选的细分选项(对于我的应用程序而言),从不同的数据源或节中填充表
  • Add each of the views in IB and lay them out on top of each other then show/hide them
  • Create each of the subviews separately in IB, then create a container in the main view to populate with the subview that you need
  • Set up one really tall or really wide UIView and animate it left/right or up/down depending on the selected segment
  • Use a UITabBarController to swap out the subviews - seems silly
  • For tables, reload the table and in cellForRowAtIndex and populate the table from different data sources or sections based on the segment option selected (not the case for my app)

那么哪种方法最适合子视图/非表方法?哪个最容易实现?您可以为该方法共享一些示例代码吗?

So which approach is best for subview/non-table approaches? Which is the easiest to implement? Could you share some sample code to the approach?

谢谢!

推荐答案

我将使用您提到的第二个选项,在IB中创建子视图,然后将它们交换进主视图中.这将是使用未分类的UIViewController的好机会:在初始设置中,使用-initWithNibName:bundle:创建一个控制器(其中第一个参数是包含单个子视图的NIB的名称,第二个参数是nil ),并根据需要将其view作为主视图的子视图添加.这将有助于保持较低的内存占用量:UIViewController的默认行为是在收到内存警告时,如果没有超级视图,则释放其视图.只要从视图层次结构中删除隐藏的视图,就可以将控制器保留在内存中,而不必担心释放任何内容.

I'd go with the second option you mention, creating the subviews in IB and swapping them in and out of a main view. This would be a good opportunity to use UIViewController, unsubclassed: in your initial setup, create a controller using -initWithNibName:bundle: (where the first parameter is the name of the NIB containing the individual subview, and the second parameter is nil) and add its view as a subview of your main view as necessary. This will help keep your memory footprint low: the default behavior of a UIViewController when receiving a memory warning is to release its view if it has no superview. As long as you remove hidden views from the view hierarchy, you can keep the controllers in memory and not worry about releasing anything.

(根据评论进行)

您不需要子类UIViewController,但是您确实需要为每个视图使用单独的XIB.您也不需要在IB的包含视图中添加任何内容.

You don't need to subclass UIViewController, but you do need separate XIBs for each view. You also don't need to add anything to the containing view in IB.

实例变量,在处理所有这些问题的类的接口中:

Instance variables, in the interface of whatever class is handling all this:

 UIViewController *controllerOne;
 UIViewController *controllerTwo;

 UIViewController *currentController;

 IBOutlet UIView *theContainerView;

在您的设置中(-applicationDidFinishLaunching:或其他)

In your setup (-applicationDidFinishLaunching: or whatever)

 controllerOne = [[UIViewController alloc] initWithNibName:@"MyFirstView" bundle:nil];
 controllerTwo = [[UIViewController alloc] initWithNibName:@"MySecondView" bundle:nil];

要切换到控制器,请执行以下操作:

To switch to a controller:

 - (void)switchToController:(UIViewController *)newCtl
 {
      if(newCtl == currentController)
           return;
      if([currentController isViewLoaded])
           [currentController.view removeFromSuperview];

      if(newCtl != nil)
           [theContainerView addSubview:newCtl.view];

      currentController = newCtl;
 }

然后只需使用

 [self switchToController:controllerOne];

这篇关于UISegmentedControl最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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