Refresh view controller content on app entering foreground - iphone

I have an app with a tab bar that has a few tabs with user-generated content. When each view controller loads, the viewDidLoad method makes HTTP requests to a server to fetch data, then populates its view correspondingly. Once all the data is loaded, however, the view remains the same (unless the view controller is unloaded and reloaded).
A lot of other apps with user-generated content has a similar behavior, except each view will reload if the app goes into the background then comes back into the foreground again. That is, as the user switches between the various tabs while the app is active, the content won't be automatically refreshed. If the user "quits" the app then comes back to it later, the views will be reloaded.
Are there standard practices for designing an app to behave this way? Specifically, I'm wondering if there are UIKit niceties that I can take advantage of to refresh my views on app "restarts".
Otherwise, I guess a straightforward approach is to have each view controller register for notifications in viewDidLoad and listen for the app entering the foreground. The controller can then respond to each notification by reloading its data.

In each of the View Controllers, you want to subscribe to the UIApplicationDidBecomeActiveNotification notification, and implement a method that gets called when it receives that notification. This method will do the reloading however your data needs to be reloaded is really beyond the scope of what we can answer here. This is how I do it, and how I recommend doing it.

Related

My textfield is getting cleared automatically

I am new to iOS development. I am having a weird situation, not every time but very often. My textfield is getting cleared automatically when I come back to the same screen using back button. Code as:
self.navigationController?.popViewControllerAnimated(true)
Problem happens in both simulator and device.
It happens because the content view is refreshed everytime when you navigate app to previous viewController this make refresh to all ui components as well. What you need to do here just save the textfields value temporally to any persistent storage like local db or using userDefaults then assign the value again using viewWillAppear() method because this method executes everytime when you navigate back your app to vc.
viewWillAppear(): Called just before the view controller’s content view is added to the app’s view hierarchy. Use this method to trigger any operations that need to occur before the content view is presented onscreen. Despite the name, just because the system calls this method, it does not guarantee that the content view will become visible. The view may be obscured by other views or hidden. This method simply indicates that the content view is about to be added to the app’s view hierarchy.

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.

Reload View only when application is opened Again

My application has tab bar with one of the tabs having a segmented control on navigation bar.
Based on segment clicked different view is displayed using a url.
I am calling the url in viewdidload method.I do not want to use viewwillAppear to call the url as it will be called each time the view is displayed.
I only want to call the url again whenever user closes the application and comes back.
Whats the best way to do this.Should I remove the view controller from and reload it again once the application is opened.
Please use a more descriptive title for your question next time!
There are notifications for this that you can observe in your application:
UIApplicationDidBecomeActiveNotification and UIApplicationWillEnterForegroundNotification come to mind.

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?