IPhone:如何在Interface Builder中创建的子视图之间切换 [英] IPhone: How to Switch Between Subviews That Were Created in Interface Builder

查看:60
本文介绍了IPhone:如何在Interface Builder中创建的子视图之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在主视图中基本上有两个子视图。我通过转到IB中的库并将视图拖到我的主nib文件然后将控件放在它们上来创建每个子视图。

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.

现在,我想要翻转通过翻转按钮在这些视图之间。我不明白的是我如何以编程方式执行此操作。

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.

我的问题是:当我进行翻转时,我是否隐藏其中一个子视图然后以编程方式取消隐藏它?我是否通过Interface Builder为每个人命名并按照这种方式进行操作?我真的不需要代码来实际执行翻转或任何操作,我只需要概念性地理解我如何以编程方式引用IB中构建的视图以及隐藏在我的场景中是否有意义...

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? 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...

有什么建议吗?谢谢

推荐答案

使用

IBOutlet UIView *连接IB中的内容myView;



@property(非原子,保留)IBOutlet UIView * myView; < br>
在头文件中。 IBOutlet 关键字告诉IB使该插座可用于连接。

You connect to things in IB by using
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.

您在连接检查器中建立实际连接通过从插座拖动到视图:
建立连接http://cl.ly/eb3b5cd826b20fc9e307/content

You make the actual connection in the Connection inspector by dragging from the outlet to the view: making a connection http://cl.ly/eb3b5cd826b20fc9e307/content

(对你的两个观点都这样做。)

(Do this for both your views.)

注意:您的观点不必在IB中的窗口。您可以在外部创建它们,并且在您需要它们之前不会显示它们。您可能希望将其中一个放入其中,以便在您的应用启动时显示。

Note: your views don't have to be inside the window in 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.

然后,当您真正想要翻转到另一个视图时,假设您是使用iOS 4.0,它很简单(有3.x和更低的方法,但这是最简单的方法):

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):

[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
                }];

这篇关于IPhone:如何在Interface Builder中创建的子视图之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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