xcode 在一个 UIViewController 中的 UIViews 之间交换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2003318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swapping between UIViews in one UIViewController
提问by Chris
I'm still finding my way around the iPhone SDK, I've followed some good tutorials but I am trying to find some simple solutions for the time being.
我仍在寻找解决 iPhone SDK 的方法,我已经学习了一些很好的教程,但我目前正在尝试寻找一些简单的解决方案。
Right now I want to knock up a simple app that will share the same UIViewController.
现在我想创建一个共享相同 UIViewController 的简单应用程序。
I have created multiple views in Interface Builder and given them unique names in the Inspector (Identity view - Interface Builder Identity).
我在 Interface Builder 中创建了多个视图,并在 Inspector(Identity 视图 - Interface Builder Identity)中为它们指定了唯一的名称。
Assuming I've placed a button and written an action called when the button is pressed. My simple question is within xcode, how do I call the one of the other views programmatically?
假设我已经放置了一个按钮并编写了一个在按下按钮时调用的动作。我的简单问题是在 xcode 中,如何以编程方式调用其他视图之一?
Many thanks
非常感谢
回答by Ben Gottlieb
I would suggest you use multiple view controllers when trying to control multiple views. Wrap them in a UINavigationController to make your life easier.
我建议您在尝试控制多个视图时使用多个视图控制器。将它们包装在 UINavigationController 中,让您的生活更轻松。
With that said, you want to create IBOutlets in your main controller, and hook each one up to one of your views. Then, when you're ready, you can
话虽如此,您希望在主控制器中创建 IBOutlets,并将每个连接到您的视图之一。然后,当你准备好时,你可以
[view1 removeFromSuperview];
[self.view addSubview: view2];
Note, this presupposes you're using a third, separate view as your controller's main view.
请注意,这假定您使用第三个单独的视图作为控制器的主视图。
回答by Alex Reynolds
Your view controller has a view
property.
您的视图控制器有一个view
属性。
You probably have two subviews of this view
property. Let's call those IBOutlet UIView *
properties subviewA
and subviewB
.
您可能有此view
属性的两个子视图。让我们将这些IBOutlet UIView *
属性称为subviewA
和subviewB
。
If they are inserted in order, then you can swap them simply by calling the -bringSubviewToFront:
method on the view controller's view
property:
如果它们按顺序插入,那么您只需调用-bringSubviewToFront:
视图控制器view
属性上的方法即可交换它们:
[self.view bringSubviewToFront:subviewB];
Likewise, to bring subviewA
back to the front:
同样,subviewA
回到前面:
[self.view bringSubviewToFront:subviewA];
If you want, you could also animate the subviews' frame
property, to move their origins relative to each other, to move them in and out of the way, as well as animate fading and other view properties. It's a bit more work, but there's a good Stack Overflow answer hereon the subject.
如果需要,您还可以为子视图的frame
属性设置动画,以相对于彼此移动它们的原点,将它们移入和移出,以及为淡入淡出和其他视图属性设置动画。这是一个有点更多的工作,但有一个很好的堆栈溢出的答案在这里的主题。
回答by Nimrod
I also strongly recommend the Apress book "Beginning iPhone3 Development".
我也强烈推荐 Apress 的书“Beginning iPhone3 Development”。
I just now implemented something like this for use with a segmented control. I have both placed one on top of the other in a subview and the segmented control action sets view1.hidden = YES; view2.hidden = NO; and vice versa in response to changed in the control state.
我刚刚实现了与分段控件一起使用的类似内容。我已经在子视图中将一个放在另一个之上,并且分段控制操作集 view1.hidden = YES; view2.hidden = NO; 反之亦然,以响应控制状态的变化。
You might want to check out the Tab Bar Controller in Interface Builder since it will switch views automatically for you with no code at all, at least in the simple case where you want to replace a screen-sized view with another screen-sized view and each have their own view controllers.
您可能想查看 Interface Builder 中的 Tab Bar Controller,因为它会自动为您切换视图,根本不需要代码,至少在您想用另一个屏幕大小的视图替换屏幕大小的视图的简单情况下,每个都有自己的视图控制器。
回答by Nithin
The book, beginning iphone3 development by Dave and Jeff has a good tutorial about this. You need to create 2 subviews of your main viewcontroller and call them separately.
这本书由 Dave 和 Jeff 开始 iphone3 开发,有一个很好的教程。您需要创建主视图控制器的 2 个子视图并分别调用它们。
回答by justin
On it's face, that's a little strange. Generally speaking, it's going to be easiest if 1 view == 1 controller.
脸上,有点奇怪。一般来说,如果 1 个视图 == 1 个控制器,这将是最简单的。
Are the views you are attempting to add sub views (that is, for instance, controls that will be added to the main view) or are they full screen views that will surplant the extant view when the user presses a button?
您正在尝试添加子视图的视图(例如,将添加到主视图的控件)还是当用户按下按钮时它们将取代现有视图的全屏视图?