Best way to load an application like it was in its previous state when it was terminated - iphone

I would like to learn the best practices in reloading the application state, such that when my app is started, it should automatically load the "correct" view/subview when it is opened again.
In my particular case, my app has a bunch of view controllers, each taking care of a UITableView. I would like for my app to "jump" to the correct node within my table view hierarchy when it is opened again.

Building on what Marc said, assuming you've got a base view controller, and then one or more levels of 'drill-down', load all your view controllers up to the 'current' one using [navigationController pushViewController: viewController animated: NO]. Then, when the user hits the Back button, they'll be presented with the pre-loaded previous view controller. A good example of this is the "Contacts" app, which preloads the Groups view controller, then pushes a view controller for the current group (usually "All Contacts") on top of that.

Check Apple's DrillDownSave sample app:
"Demonstrates how to restore the user's
current location in a drill-down list
style user interface and restore that
location when the app is relaunched."
http://developer.apple.com/iphone/library/samplecode/DrillDownSave

You'll need to associate an identifier with each view controller, and save that value to the user defaults when the current view changes. The next time you launch, read that value and you'll know which view controller you need to load initially.

I've published a little library to help doing these kinds of things. I'm already using it in an app I've published on the App Store and it's a lot smoother than implementing NSCoder for every view manually :P

Related

Iphone knowing when going back in navigational view control

I'm using a navigation based view controller on my iPhone app that uses an RSS feed on-load of a nib to bring back results, then another xml call when you select one. Is there an action when you go back on the second view controller to the first, the reason I ask is that it's reloading the first results again (going off to the server) as it's in the viewdidappear. How do I tell it not to re-load the results?
Tom
Move the code to the ViewDidLoad it is only called when the view gets loaded.

Reloading subviews of UINavigationController

I'm kind of new to iPhone development so bear with me.
I have an application wherein I display a lot of data in a tableviews, edit it in a detailview etc. However, I also have a login-system.
The problem I have is that I can't figure out how to reload the subviews of the NavigationController when I've logged out, or how to dealloc it completely and reinitialize it upon a succesful login.
This means that data from the last user who logged out is still present in my tableview when I log in as another user, as the data is set to reload only when the view loads for the first time.
Thankful for any and all contributions.
There are probably several ways you could go about this; it's up to you (and without more information about your app, I can't suggest a particular solution). You might consider:
-viewWillAppear: — this method is called on any UIViewController subclass when it's about to (re)appear as part of a UINavigationController stack (or tab bar controller, etc.). You can clear out fields, etc. This is mostly useful when a view controller reappears (being uncovered or switched to), because you usually create a new view controller instance each time you display one.
Notifications and delegates — your view controllers (and other objects) can communicate with each other about when a logout occurs, and reconfigure themselves as necessary.

Good design for having an ActivityIndicator across entire app

I have the need for an activity indicator view in my app when different views are loading and when data is being retrieved. The problem is the mainVC (where I would place the indicator) is not always aware of when processing is happening so it can start the indicator but it cannot stop it.
e.g. the mainVC loads and then programatically adds a new VC - this VC in turn asks a model to retrieve - it displays data etc. So this newly added VC actually knows when processing is finished and it does not have access to the indicator view (although the indicator is visible at the top).
I was thinking of using notifications - is this the best way of handling this situation?
I'd recommend looking at the brilliant MBProgressHUD library:
https://github.com/matej/MBProgressHUD
It's a very simple set of classes you can use to display loading and progress views that can be accessed by all view controllers in your app. Basically, you can set it up in your app delegate and add it to your app window.
Every view controller can then access the progress view from the delegate and show/hide it when required. It comes with an example project and code - it's very easy to use and customise.
Notifications are one half of the solution. You have to combine them with a persistent object so that you can also get the current state at all times. E.g., when a view controller is about to appear, it needs to read the initial "downloading" state from somewhere, because the VC might have been created after the "start" or "end" notification was sent.
Then, while the VC is alive, it can simply respond to notifications to update the indicator.
This design is particulary important for views, which run the risk of getting unloaded/reloaded all the time.

iOS Table Views and View Controller

I am trying to write my first iPad app, and I have a problem.
I have my Main view controller with a button. Once the button is pressed, it requests a list of information from a web server, and returns the data. This data, I want to put into a table view in a popover.
I have the main view controller, and the data is received back and put into a dictionary, then that is looped through to get a couple of arrays of data.
I also have a Popover view controller that can display a static table view.
My question is, how do I get the data (either the dictionary, or the arrays) from the Main view controller to the Popover view controller?
I am really lost.
Thanks
James
There are two design patterns you have to keep in mind: delegation and notification. in a more general way also KVO is useful to update your view if the model changed. Of course the object listening for notifications or acting as a delegate is the controller of that view, NEVER the view itself.
There are two things you want to avoid at any time: storing stuff in global variables or in singletons and making "super objects" that act as let us say the model and the controller. Stick to the MVC paradigm and try to loose couple your objects and think about which object owns which other object. This is important for memory management and if you not implement it correctly it will come back to you later.
Check out session #116 - Model-View-Controller for iPhone OS from WWDC10 session videos and session #123 - iPhone View Controller Techniques from WWDC09.
The WWDC10 videos are available for free at apple's developer site.
Hey James, I suggest you first do your homework and try to get a taste on how so-called delegate structure works in most iOS apps.
To be more clear, your main view controller would become the delegate and your popover view controller would become the child of such a delegate. The delegate is responsible for presenting its child controller. At the same time, the child controller is responsible for asking for data from and reporting any changes to its delegate controller.
So in this way your popover controller can get the data it wants from your main view controller, and at the same time, when the user wants to dismiss the popover view, the popover view controller detects the user's instructions and informs the main view controller to dismiss it.
have described another possibility a few seconds ago:
Passing array between view controllers?

How do I access the view that is one level up from my current view in a navigation-based iOS app?

I'm diving into iOS development and am getting familiar with navigation view controllers. I'm trying to build a simple app with a table view that allows me to add objects to it. So far, I have a table view with an add "+" button in the nav bar that allows me to load my CreateObjectView and display it modally so the user can define the new object, but when they click the save button, I don't know how to send that object data back to the parent view that created the CreateObjectView that contains the object data.
When I create the child view (CreateObjectView), I could pass into it a pointer to the current view before I push it onto the nav stack, but this just feels dirty and circular.
I was reading about protocols and delegates and I could use that approach as well, but it also feels circular.
This question seems like it would be a common design issue to any tableview-based or nav-based app, how should I probably access the view that is one level up from my current view in a nav-based iOS app?
Thanks so much in advance for all your help!
It feels circular at first, but it is the right way. Before the UINavigationController pushes the new view controller you should set the delegate to the current view controller - assuming that's the object you wish to communicate with. Of course you could set it somewhere else instead, such as to a central Core Data handler. Then when you need to communicate call the delegate with the method you have defined. I've done several navigation apps like this after seeing Apple's cookbook example, and it really works well.