views:

353

answers:

2

Hello everyone,

I am trying to understand the behavior of view controllers when switching from one to another (displaying different views)

A part form the addSubiew statements which seem to work, I can't find an explanation to what happens with the two statements:

self.view = someViewController.view; [someViewController loadView];

In fact I got a case where only the first one seems to work (the view defined within someViewController is displayed) and in another case only the second one.

More precisely, from the root viewController.view to anotherViewController.view (already istantiated) I have to use the first one, to come back I need to use the second one. I can't understand what can it be the difference in the current situation which allows one or the other statement to work.

Thank you

+2  A: 

It's very hard to even understand your question. But I'm gonna try to answer it:

The iPhone OS uses a stack of views and display the first one to the users. You either pop (remove) or push (add) views to this stack.

The code you provided is somewhat "obscure". The best pratice to gain control is to add SubViews to your window or any other view

i.e. [window addSubview:viewController.view];

Don't override your current view by using self.view = someViewController.view; Instead initialize your new ViewController and add it's view to the subview of your current view.

Any questions? Just comment.

Henrik P. Hessel
Thank you very much for your attention and your response. I apologize if my post resulted not clear. It was my first posting ever, and I tried to be as much synthetic as possible, maybe it was a little too synthetic...Anyway, I am glad to read thatself.view = someViewController.view;is not a good practice, in fact, even if it seems to reach the goal of displaying another view, it didn't make sense to me.I will keep on trying with the addSubview technique.Thank you again
+2  A: 

Looks like you have some misconceptions about how view controllers work.

There are a couple things wrong with those 2 statements:

self.view = someViewController.view;

According to the docs of UIViewController.view :

"Each view controller object is the sole owner of its view. You must not associate the same view object with multiple view controller objects." -Apple Docs

Once that line of code executes, the view would have 2 different controllers, which is bad.

Next line:

[someViewController loadView];

This is bad because you should never explicitly call loadView.

From the docs of UIViewController.loadView :

"You should never call this method directly."

From the docs of UIViewController.view :

"If you access this property and its value is currently nil, the view controller automatically calls the loadView method and returns the resulting view. "

Spend some time with the viewController tutorials and guides such as "View Controller Programming Guide for iPhone OS". They are good docs and can teach a lot.

CornPuff
Thank you very much for your response.I'll certainly carefully consult the documentation you have indicated.