开发者

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.

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? 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 se开发者_运维知识库nse in my scenerio...

Any suggestions? thanks


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.

You make the actual connection in the Connection inspector by dragging from the outlet to the view:

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

(Do this for both your views.)

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.

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


Animations are done programatically. (always) So you need a reference

in you @interface write something like this:

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.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜