views:

467

answers:

5

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

+3  A: 

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.

Ben Gottlieb
Knock up means "build" in the UK and Oz. Used "...knock up a shed."
willc2
Ah, well then, I stand corrected!
Ben Gottlieb
Hey that's great, thanks. I'll be checking the book that's been recommended twice and I'll look into using separate view controllers. I appreciate the pointer
Chris
A: 

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?

justin
It will be separate interfaces, it will eventually be a simple enough game, just testing the fundamental controls of iPhone SDK before hand, I'm quite new to delegation but I suppose even if 1 view controller is used per view I can still make one more dominant? I'll dig deeper, I appreciate all the feedback
Chris
+1  A: 

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.

Alex Reynolds
Thanks, I presume this is if both sub views are already sitting in view though? It's not how this app will work but that's very handy to know! Cheers
Chris
A: 

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.

Nithin
+1  A: 

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.

Nimrod
Many thanks I will fetch that book
Chris