简体   繁体   English

iPhone:如何在 Interface Builder 中创建的子视图之间切换

[英]IPhone: How to Switch Between Subviews That Were Created in Interface Builder

So I basically have two subviews within my main view.所以我的主视图中基本上有两个子视图。 I created each subview by going to the library in IB and dragging a view onto my main nib file and then positioning controls on them.我通过转到 IB 中的库并将视图拖到我的主 nib 文件上,然后在它们上放置控件来创建每个子视图。

Now, I'd like to flip between these views via a "flip" button.现在,我想通过“翻转”按钮在这些视图之间翻转。 What I don't understand is how I can programmatically do this.我不明白的是我如何以编程方式做到这一点。

My question is: do I "Hide" one of the subviews and then unhide it someway programmatically when I do the flip?我的问题是:我是否“隐藏”一个子视图,然后在翻转时以编程方式取消隐藏它? Do I give each a name via the Interface Builder and do it that way?我是否通过 Interface Builder 给每个人一个名字,然后这样做? I don't really need the code to actually do the flip or anything, I just need a conceptual understanding of how I'd refer to views built in IB programmatically and if hiding makes sense in my scenerio...我真的不需要代码来实际执行翻转或任何操作,我只需要概念性地了解我如何以编程方式引用 IB 中构建的视图,以及隐藏在我的场景中是否有意义......

Any suggestions?有什么建议? thanks谢谢

You connect to things in IB by using您通过使用连接到 IB 中的事物
IBOutlet UIView *myView;
or或者
@property (nonatomic, retain) IBOutlet UIView *myView;
in your header file.在你的头文件中。 The IBOutlet keyword tells IB to make that outlet available to connect. IBOutlet关键字告诉 IB 使该插座可用于连接。

You make the actual connection in the Connection inspector by dragging from the outlet to the view:通过从出口拖动到视图,您可以在连接检查器中建立实际连接: 建立联系

(Do this for both your views.) (根据您的观点执行此操作。)

Note: your views don't have to be inside the window in IB.注意:您的视图不必位于 IB 的窗口内。 You can create them outside, and they won't be displayed until you want them to.您可以在外部创建它们,除非您希望它们显示,否则它们不会显示。 You might want to put one of them in so it shows up when your app launches.您可能希望将其中一个放入其中,以便在您的应用程序启动时显示。

Then, when you actually want to flip to the other view, assuming you're using iOS 4.0, it's simple (there are methods for 3.x and lower, but this is the easiest):然后,当你真的想要翻转到另一个视图时,假设你使用的是 iOS 4.0,这很简单(有 3.x 及更低版本的方法,但这是最简单的):

[UIView transitionFromView:myView1
                    toView:myView2
                  duration:0.2
                   options:UIViewAnimationOptionTransitionFlipFromRight
                completion:^{
                    // something to do when the flip completes
                }];

Or, if you want to dynamically determine which view is already visible:或者,如果您想动态确定哪个视图已经可见:

UIView *oldView, *newView;
UIViewAnimationOptions transition;
if (myView1.superview) { // view 1 is already visible
    oldView = myView1;
    newView = myView2;
    transition = UIViewAnimationOptionTransitionFlipFromRight;
} else { // view 2 is visible
    oldView = myView2;
    newView = myView1;
    transition = UIViewAnimationOptionTransitionFlipFromLeft;
}
[UIView transitionFromView:oldView
                    toView:newView
                  duration:0.2
                   options:transition
                completion:^{
                    // something to do when the flip completes
                }];

Animations are done programatically.动画以编程方式完成。 (always) So you need a reference (总是)所以你需要一个参考

in you @interface write something like this:在你@interface写这样的东西:

IBOutlet UIView subview1;
IBOutlet UIView subview2
IBOutlet UIView mainView; //this depends on your structure, may be self or if you are in a controller self.view

You need to connect these views references with the iB views.您需要将这些视图引用与 iB 视图连接起来。

Then inside the button's action (method) just do:然后在按钮的操作(方法)中执行以下操作:

[mainView bringSubviewToFront:subview1];

If you need animations you could check the code of Utitlies Template Project of Xcode.如果您需要动画,您可以查看 Xcode 的 Utilities Template Project 的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM