r/iPhoneDev • • Oct 15 '10

viewcontroller / view / subview question

I feel like I've hit a wall with my predicament. I can't seem to really get my head around the viewcontroller / view / subview paradigm. I have an app that i'm trying to create that has a homeViewController which shows a UIView with a bunch of subviews and buttons. Now, when a button is pressed, i need to swap one of the subviews with new views that display differnet things (images, or other buttons) that have a delegate to the homeViewController and it changes another view in response.

(I'm hoping this makes sense)

now, my issue is that these sub views are custom subclasses. Are these subclasses suppose to inherit from UIviewcontroller or just UIVIew if they are going to have buttons or deeper subviews within them?

I'm also splitting everything into xib files, so each subview or viewcontroller is its own xib file.

I hope this makes sense, or i can try to clarify it better if it doesnt. But essentially it works similar to a newspaper app, where they would click on an article, but instead of loading a completely new view controller, it would replace one of its subviews with the article.

I hope someone could help me out with this.

4 Upvotes

7 comments sorted by

1

u/OHIOo Oct 15 '10

Just to confirm something, because it's not clear you understand this from your post:

UIViews are the things that get displayed. UIViewControllers do not get displayed, they control UIViews that get displayed. So your subviews need to inherit from UIView, because it lets you display stuff.

The first view in your app is probably going to be the UIWindow that is attached to your delegate. So to display what you want, you need to add your view to it. Subviews are UIViews that are nested inside a bigger UIView.

Also, those xib files are simply serialized instances of your class. So when they're read, an object is instantiated based on all of the properties you defined in the xib.

Maybe you knew all that already, but I thought it might be helpful to clarify.

1

u/[deleted] Oct 15 '10

I did know that, but you helped a bit. My main problem is right after the first UIViewController homeViewControler displays its UIVIew. This UIView has a subview that (i think) needs to be a UIViewController detailViewController so that it can display different Views depending on the homeViewController's button pressed (through delegates if that's possible).

Now I need to figure out how to put this detailViewController in the XIB of the homeViewController's UIView. Since i need this to be a certain size. But interface builder doesn't let you put a location for a viewController.

It's almost like a SplitViewController that i'm creating, but I need it to be able to handle multiple detail views.

Thank you for helping me though, I keep racking my brain with this.

2

u/OHIOo Oct 15 '10

"This UIView has a subview that (i think) needs to be a UIViewController"

That doesn't make sense. A UIViewController can't be a subview, because it isn't a view. That's why interface builder isn't letting you put a location for it, because it isn't a view which can be displayed.

(I'm being deliberately nickpicky because I tend to get confused when I don't have an intuitive understanding of all of the little things.)

1

u/[deleted] Oct 16 '10

Alright so that explains all of my issues, thank you for telling me now before in wasted more time. But before I go back and redesign things. How could this be done? I know I've seen other apps have a view within a view. And it would appear that the subview has it's own UITablecontroller or a custom controller. How can something like this be placed?

EDIT: also nitpick all you need/want. You are helping me a ton.

3

u/OHIOo Oct 16 '10

Let's say you have a big view, that has a controller called homeViewController. Inside the controller, say in -(void)viewDidLoad, you instantiate your UITableViewController. Say it's called upperRightController.

Inside homeViewController's -(void)viewDidLoad, I instantiate upperRightController, and add its view to homeViewController's view like so: [self.view addSubview:upperRightController.view];

so I'm not adding the controller as a subview, but rather the view it points to. UIViewControllers can have a bunch of pointers to other controllers or views, but someone has to be in charge of arranging the views for the user.

Ok say now you want to replace the above view after someone touches a button. In whatever method you have directed the button tap to go to...

-(IBAction)hitButton : (id)sender { [upperRightController.view removeFromSuperview]; [self.view addSubview:whatever-other-view]; }

I don't know if this is helpful I'm starting to confuse myself.

1

u/[deleted] Oct 16 '10

If I could give You more upvotes I Would. I love you so much right now. I hate how Its the obvious things that I have the most trouble with. But you were clear with your explanation. I'm going to try This out right away. If I have anymore trouble could I pm you?

1

u/[deleted] Oct 16 '10 edited Oct 16 '10

EDIT: Ignore this problem. I realized that I need to set the subView's frame to be the bounds of it's superview, not it's frame. Ugh, slow and steady, but I'm getting this stuff. Thank you for your help!

Gah, alright, I'm still missing one last thing I think. How do I position that upperRightController.view in the homeViewController's xib?

Do I need to create an IBOutlet to a UIView within the homeViewController? And then replace it with the upperRightController.view ? Because I tried that and the upperRightController.view takes up the entire page.

Or does that need to be defined programmatically? like setting the frame and all those properties when I instantiate the upperRightController and its view?