开发者

UINavigationController - When to release pushed view controllers, etc

Apologies for the big block 'o' text, but I'm somewhat of a beginner having a play around with using a UINavigationController as a part of an app I'm writing and I'm just wondering how I should handle adding view controllers to a UINavigationController and when (if ever) I should release them.

As I see it, there are two potential approaches when adding a view controller:

  1. Have an instance variable for each view controller in the managing class. Initialise it (if the instance variable is nil) prior to pushing it onto the nav controller. Subsequently release the view controller in the dealloc method of the managing class if it isn't nil.

  2. Create the view controller as a local variable within a method and push it onto the navigation controller prior to immediately releasing it.

However, I'm unsure of the validity of each of these. (My gut feel is to go with the instance variable approach, but perhaps that's due to a lack of understanding on my part.)

Digging a bit deeper, I suspect that my confusion is related to the fact that I don't quite grasp whether or not I'm responsible for disposing of the view controllers that are pushed onto the navigation controller stack, or whether it'll dispose of them itself when they're popped off its view controller stack. (e.g.: When the user hits the 'back' button.)

If it's the former, then I can see that I'll need to use the instance variable approach and implement the UINavigationControllerDelegate methods in the managing object to determine whe开发者_JAVA百科n I should release and nil each view controller, etc.

Finally (for bonus points in the afterlife) how can you easily tell if a method will increment the retain count of one of its parameters? (There don't appear to be any clues in the documentation, but perhaps I'm missing something obvious.) For example, when I use something along the lines of...

[[self navigationController] pushViewController:exampleVC animated:YES];

...will this increase the retain count on the view controller? (Once again, my gut feel is that it should, but that's about as much use as a chocolate fireguard in these dangerous times, etc.)

Thanks in advance.


That's pretty simple. If you hand off an object to another object that does something with it that's not under your direct control it retains the object you pass (pretty much everything works like that).

For Example: All NSArray/Dictionary/Set Collections retain your objects because if you would deallocate a object contained in such a record some entries would be invalid and the collection would not know.

Exactly the same is true for navigation controllers and such things, as you don't know if and when your viewcontroller is shown or discarded.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜