Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-09-2009, 06:38 AM   #1 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 29
Default Handling and accessing view controllers in nav controllers throughout app's lifespan

Hi there,

I have a Navigation Controller with a Table View Controller as it's root. This table view lists some data from a database which is stored in an array. This array is a property of the Table View Controller. When a row is selected, this the row id is passed to a View Controller (that is pushed onto the Nav Controllers stack) that handle's the full editing of that record. On load, it pulls in all the remaining data to do with that record (that is not needed by the Table View for just displaying cells).

Once the row is edited, and the user presses a button, I want to be able to somehow get the previous navigation controller and tell it to update it's records and call the reloadData method on the Table View.

My question is how can you access previous View Controllers? Should you create "global" pointers (say in the app delegate) to all the main View Controllers in your app? As these views are always retained during the app's lifespan by the main navigation controller, is this such a bad thing to do? This way you can always refer to a particular controller and tell it to update?

I guess this is a general question about how to structure all the View Controllers in apps. Whether you just alloc+init them, add them to the Navigation Controller and release them, or whether you always keep pointers to them so you can fully control them from anywhere during the apps lifespan.

Many thanks for your help!

Michael
Bisbo is offline   Reply With Quote
Old 05-09-2009, 08:06 AM   #2 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Do you really need to do this updating from the 2nd level view controller? Or do you just need to make sure the main view controller updates itself when you pop back to it?

I use - (void) viewWillAppear: (BOOL) animated in my upper level view controllers to refresh data after return from a lower level view controller.

joe
FlyingDiver is offline   Reply With Quote
Old 05-09-2009, 08:27 AM   #3 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 29
Default

I had thought about that, but I'll need to be able to find out whether any data has changed or not, as the reloading of data can take some time and it should only happen if anything has changed.

As the data structures for the table view are in the table view controller, I'll somehow need to access that view controller to tell it if anything's changed.
Bisbo is offline   Reply With Quote
Old 05-09-2009, 09:57 AM   #4 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Quote:
Originally Posted by Bisbo View Post
I had thought about that, but I'll need to be able to find out whether any data has changed or not, as the reloading of data can take some time and it should only happen if anything has changed.

As the data structures for the table view are in the table view controller, I'll somehow need to access that view controller to tell it if anything's changed.
The data is in which controller? The top one or the 2nd level one? Since the top one already has a pointer to the 2nd level controller, maybe it should query it to see if anything has changed? Then reload or not as needed.

This might be a good argument for moving the data to a data source object.

joe
FlyingDiver is offline   Reply With Quote
Old 05-09-2009, 10:18 AM   #5 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 29
Default

Well I have 2 seperate classes to hold the data, one class just holds the data needed to display a table row's worth (these objects are stored in first view controller), and the other holds the full object (with lots and lots of data) which is held in the 2nd controller. This is done for optimisation reasons as hundreds of records need to be pulled into the table. The root view controller just passes the record ID to the second view controller, which then pulls all the data required to display it in full.

Is this a good solution? I think if I could somehow reference any view controller at any time I could tell it that things have changed, and if so, what, so it immediatly knows what objects to reload with data from the database.

Am I making sense? I hope I am!

Thanks, for your help!
Bisbo is offline   Reply With Quote
Old 05-09-2009, 10:21 AM   #6 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 29
Default

There must be a way of getting to all the view controllers precisely, and from anywhere in the app. Would it be a good solution to add pointers to the delegate to all the main view controllers in the app? It may only be 5 or 6, and these will always be retained by the navigation controller so there's no memory overhead at all?
Bisbo is offline   Reply With Quote
Old 05-09-2009, 10:42 AM   #7 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

You don't need to store pointers to the delegate in the view controllers, you can just do something like:

Code:
	SatPointerAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	[delegate.rootViewController update];
This way, if you have an "update" method in that view controller, you can force an update anytime. And you might want that view controller to have a list of all it's sub controllers, and force each of them to do an update from that same method. Just iterate through the list, sending an update (or even just "reload") method to each.

You really don't want to be maintaining lots of view controller pointers in the different controllers, because it makes a real mess if you add or delete one.

It also sounds like you're using a database or some other external storage for your data, and accessing it from each view controller to get what that controller needs. I would suggest that you encapsulate all that in a custom class (model class for that data). Then if you need to change the storage mechanism for that data, only the one class needs to know. You might decide to do that to migrate to CoreData in 3.0, for instance.

joe
FlyingDiver is offline   Reply With Quote
Old 05-09-2009, 10:51 AM   #8 (permalink)
Registered Member
 
Join Date: May 2009
Posts: 29
Default

Yes I'm encapsulating all my data connections in preparation for CoreData! Okay, I think I know where I'm going with this now! Thanks for your help!
Bisbo is offline   Reply With Quote
Old 05-09-2009, 10:56 AM   #9 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

You don't need to store pointers to the delegate in the view controllers, you can just do something like:

Code:
	SatPointerAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
	[delegate.rootViewController update];
This way, if you have an "update" method in that view controller, you can force an update anytime. And you might want that view controller to have a list of all it's sub controllers, and force each of them to do an update from that same method. Just iterate through the list, sending an update (or even just "reload") method to each.

You really don't want to be maintaining lots of view controller pointers in the different controllers, because it makes a real mess if you add or delete one.

It also sounds like you're using a database or some other external storage for your data, and accessing it from each view controller to get what that controller needs. I would suggest that you encapsulate all that in a custom class (model class for that data). Then if you need to change the storage mechanism for that data, only the one class needs to know. You might decide to do that to migrate to CoreData in 3.0, for instance.

joe

joe
FlyingDiver is offline   Reply With Quote
Reply

Bookmarks

Tags
navigation controllers, view controllers

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 264
17 members and 247 guests
14DEV, @sandris, ADY, ArtieFufkin10, bookesp, ckgni, Dani77, DarkAn, HemiMG, iDifferent, jakerocheleau, JasonR, prchn4christ, Rudy, Speed, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,230
Posts: 380,767
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 02:49 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0