views:

525

answers:

2

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 sense in my scenerio...

Any suggestions? thanks

+1  A: 

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.

nacho4d
+3  A: 

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: making a connection

(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
                }];
jtbandes
Thank you for your detailed explanation. I don't quite understand what you meant by creating the views outside the main nib file though. I'd like to be able to create a view in IB but I thought I could only do this from inside my main nib. Maybe I'm missing something... ?
Shnitzel
Not outside the main nib file, but outside the window. Notice in my screenshot how "My View 1" is in the list with the rest of the nib's objects — it's not actually placed inside the window. You should use this to your advantage so only one of the views is onscreen (in the window) at a time.
jtbandes
Ok I get you. I achieved this by dragging my subview from inside my main window to outside where your screenshot shows. (I didn't know I could do this!)
Shnitzel
Yep! It's a useful way to create views without adding them to the window yet. So, I suggest you put your 1st view into the window but leave the 2nd one out, then when you switch them in code it'll be the other way around.
jtbandes
How do I check if -transitionFromView method is available or not? It is present in OS 4.0 and above but not in lower versions. I dont want to fetch the OS version, but something like -respondsToSelector will be good. Alas -respondsToSelector is an object method and hence cannot be used on UIView. What is the solution?
Raj
`-respondsToSelector` can be used on classes too.
jtbandes