问题
Thanks for all your help, as usual, i am looking for some more information...
Regarding view controllers, i am trying to develop an application with multiple views.
View Controller loads from View A using presentModalViewController - and loads View B from there - also works fine, but i have some nagging questions...
View A has a table in it, which drills down to View B, and i can move back to view A usig dismissModalViewController, but once back in View A i can't access the table in View A.
What happens to controls/variables in View A when View B is pushed? is the dealloc called when View B is called? If i wish to access controls/variables when View B is dismissed and View A comes back into view, what do i need to do?
Is there some article that can educate on this? ANy help is highly appreciated
To re-iterate
- View Controller initiates a page from View x
- View X loads - View A with a table into the view using presentModalViewController
- table is released in View A's dealloc
- View B loads fine, works fine - dismissing the view B controllers takes back to View A
- At this point, i would like to reload the table in View A once it comes back onto the screen
I am pretty sure there are many other fellow members who are in this kind of situation
Here is my code, in bits and pieces
startPage - (This is the application's home page) with a Start Button, contains
View Controller -> View
On clicking the start button, am loading a viewcontroller called ViewA(View Controller -> View), with the following code
UIViewController *viewA= [[viewA alloc] initWithNibName:@"viewA" bundle:nil];
viewA.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
[naviControl presentModalViewController:viewA animated:YES];
[viewA release];
On picking a table row in ViewA, another viewcontroller ViewB(View Controller -> View) is called with the follwoing code
UIViewController *viewB= [[viewB alloc] initWithNibName:@"viewB" bundle:nil];
viewB=UIModalTransitionStyleCoverVertical;
[self viewB animated:YES];
ViewB is dismissed and VIewA is shown with code
[self dismissModalViewControllerAnimated:YES];
At this point, with some NSLogs, when ViewB is dismissed, only viewWillAppear is called on ViewA, not viewDidLoad and if i try to reload data on the table in ViewA, the app crashses with EXC_BAD_ACCESS
Thanks Veeru
回答1:
First of all, please correct following statement:
UIViewController *viewA= [[viewA alloc] initWithNibName:@"viewA" bundle:nil];
to
UIViewController *viewA= [[UIViewController alloc] initWithNibName:@"viewA" bundle:nil];
and secondly you will need to understand the memory management and flow of view controllers, e.g.
- when you allocate a view controller then an instance with retain count 1 is initiated in the memory,
- when you push or present this view controller on to the screen then its retain count becomes 2 as now another instance either navigation controller or view controller is using your instance,
- then you release it this makes retain count of your view controller's instance again to 1, i.e. reduces by 1, (you must be aware that when the retain count becomes 0 then only it will be freed from memory).
- then you have called another view controller on top of this current view controller which makes it to go in background and as you haven't retained the instance explicitly it is getting released by your nav controller or view controller in which you added, so its retain count becomes 0, hence freed from memory.
- so when you try to reload tableview it is giving you EXC_BAD_ACCESS as you are trying to use an instance which is already freed.
Solution:
If you are trying to use multiple navigation screens in your application, you should only use pushViewController method of UINavigationController which keeps the instances of viewcontroller until you explicitly release them.
Also be careful of using local instances or object instances and their retain count, keep track of alloc and release i.e. retain count, it should not be 0 unless you want them.
Hope this will help...
回答2:
When you present ViewController(VC) B as a modal from VC A it should not dealloc any of VC A variables(unless maybe a Memory Warning is sent). If you want to reload table data that resides in VC A when you dismiss B then you can use [UITableView reloadData]. You should call this from the viewDidAppear method since this will be called anytime the view is coming back onto the screen. Hope this helps!
ps.. replace UITableView with appropriate table to reload.
来源:https://stackoverflow.com/questions/4226040/iphone-understanding-the-view-controller-presentmodalviewcontroller