从UISegmentedControl更改视图 [英] Changing Views from UISegmentedControl

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

问题描述

我有一个关于Nibs和UISegmentedControls的最佳练习类型。

I have a best practice type of question regarding Nibs and UISegmentedControls.

我有一个nib文件,其中包含一个带3个按钮的UISegmentedControl。当切换时,每个视图在窗口的主要部分显示不同的视图。

I have a nib file which contains a UISegmentedControl with 3 buttons. Each of which displays a different view in the main part of the window when toggled.

一切正常但我怀疑我的方法存在缺陷,并且想知道是否有人可以建议这是我意味着这样做的方式。

Everything is working fine but I suspect my approach is flawed and was wondering if someone could suggest the way I was "meant" to do it.

目前在NIB中我有3个UIViews叠加在一起,然后在.m文件中我修改每个UIViews .hidden以隐藏和显示我感兴趣的那个。

Currently in the NIB I have 3 UIViews which are overlayed on top of one another and then in the .m file I modify each UIViews .hidden to hide and reveal the one I am interested in.

- (IBAction)segmentAction:(id)sender
{
    if([sender selectedSegmentIndex] == 0)
    {
        [self.view1 setHidden:NO];
        [self.view2 setHidden:YES];
        [self.view3 setHidden:YES];
    }
    else if([sender selectedSegmentIndex] == 1)
    {
        [self.view1 setHidden:YES];
        [self.view2 setHidden:NO];
        [self.view3 setHidden:YES];
    }
    else if([sender selectedSegmentIndex] == 2)
    {
        [self.view1 setHidden:YES];
        [self.view2 setHidden:YES];
        [self.view3 setHidden:NO];
    }
}

一切都有效但是nib文件很痛苦编辑,因为你不能轻易关闭其他UIViews所以我总是在画布上选择错误的元素。此外,IB抱怨说:这个视图与其中一个兄弟姐妹重叠在信息对话框中。

Everything "works" but the nib file is a pain to edit as you can't easily "turn off" the other UIViews so I invariably select the wrong element on the canvas. Also IB complains that: "This view overlaps one of its siblings" in the info dialog.

我应该采取什么方法,因为我希望所有的在InterfaceBuilder中布局的视图。我应该有4个nib文件吗?如果是这样,我应该在按下segmentControl时加载它们?您可以指示我的任何示例代码?

What is the approach I should be taking given that I would like all of the views to be laid out in InterfaceBuilder. Should I have 4 nib files? If so how should I load them in when a segmentControl is pressed? Any sample code you can direct me to?

谢谢

推荐答案

在我的脑海中,您可以尝试以下几种方法:

off the top of my head, here are a couple things you can try:

1)在IB中制作3个独立的UIView对象,但不要将它们添加到视图中heirarchy

1) make 3 separate UIView objects in IB, but don't add them to the view heirarchy

假设您有一个视图控制器,其中包含所有三个视图,您可以在IB中通过将每个视图从库拖动到窗口来创建3个视图标题为YourNib.nib或YourXib.xib。所以不要将视图拖动到视图控制器的主视图中。

Assuming that you have a view controller where all three views are contained, you can, in IB, create the 3 views by dragging each view from the library to the window titled YourNib.nib or YourXib.xib. So don't drag the views into the view controller's main view.

现在您可以双击每个视图并自行编辑它们,而不必尝试选择不同的视图,因为它们在连接选项卡中重叠(因为它们不是)

Now you can double click each one and edit them on their own, without having to try to select the different views because they're overlapped (because they aren't)

,将视图控制器的视图属性连接到其中一个视图。

in the connections tab, connect the view controller's view property to one of the views.

这设置了默认值。

现在在您的代码中,您可以更改连接的视图:

now in your code, you can change which view is connected to:

[myViewController setView:view2];

[myViewController setView:view2];

并重新加载视图。

2)制作一个非常高的视图(或非常宽的视图)并在单击分段视图按钮时更改视图的y位置,使其对用户显示为好像它们是转到单独的页面

2) make one really tall view (or really wide view) and change the y-position of the view w/ respect to the parent view's frame when you click a segmented view button so that it appears to user as if they're going to separate pages

这使得在视图之间动画和转换变得容易。虽然会占用更多内存,因为即使它们不可见,也会加载所有元素。但无论如何你都是这么做的。

this makes it easy to animate and transition between "views". though will use more memory because you have all elements loaded even when they're not visible. but you were doing that anyway.

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

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