Chris
Chris

Reputation: 2488

Swapping between UIViews in one UIViewController

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.

Right now I want to knock up a simple app that will share the same UIViewController.

I have created multiple views in Interface Builder and given them unique names in the Inspector (Identity view - 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?

Many thanks

Upvotes: 1

Views: 5972

Answers (5)

Ben Gottlieb
Ben Gottlieb

Reputation: 85542

I would suggest you use multiple view controllers when trying to control multiple views. Wrap them in a UINavigationController to make your life easier.

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

[view1 removeFromSuperview];
[self.view addSubview: view2];

Note, this presupposes you're using a third, separate view as your controller's main view.

Upvotes: 3

Nimrod
Nimrod

Reputation: 5133

I also strongly recommend the Apress book "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.

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.

Upvotes: 2

Nithin
Nithin

Reputation: 6475

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.

Upvotes: 0

Alex Reynolds
Alex Reynolds

Reputation: 96976

Your view controller has a view property.

You probably have two subviews of this view property. Let's call those IBOutlet UIView * properties subviewA and 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:

[self.view bringSubviewToFront:subviewB];

Likewise, to bring subviewA back to the front:

[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 here on the subject.

Upvotes: 3

justin
justin

Reputation: 868

On it's face, that's a little strange. Generally speaking, it's going to be easiest if 1 view == 1 controller.

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?

Upvotes: 0

Related Questions