0

I have a pretty simple tabbed application with two tabs 'Current Info', and 'Settings'. In the settings tab you can configure how often to poll a service for information. Once you configure polling the app will poll the service and update info in the 'Current Info' tab.

Simple problem probably. I have a TableViewController for the settings panel.

#import <UIKit/UIKit.h>

@interface SettingsViewController : UITableViewController <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *refreshRateTextField;

@property (copy, nonatomic) NSNumber *refreshRate;

@end

What is the recommended way to get the refreshRate passed to the other ViewController. Not that it matter but the other ViewController is also a TableViewController. Because the Current Info panel needs to update the interval in which it polls if the setting changed I probably want to fire an event to the CurrentInfoController such that it can update its interval.

I am very new to iOS (this is a simple startup app I am trying to write). As such a high level answer or pointing me to apple docs on this sort of problem would go along way, just need to knwo how to get started.

Also forgot to mention that I am using storyboards, a lot of MVC examples for this use nib files.

Thanks.

lostintranslation
  • 23,756
  • 50
  • 159
  • 262
  • Probably best you check this out :) http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – simonthumper May 20 '13 at 15:49
  • If you can manage it, the best solution in this case is to stop polling altogether. Use push notifications to tell your app that new data is available. I realize that that's not always possible, but it's certainly the preferred solution. – Caleb May 20 '13 at 16:50
  • Yup, this was a pretty good explanation. http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers – lostintranslation May 20 '13 at 17:30

2 Answers2

0

When communicating to other view controllers, you could use NSNotifications, however in this situation, NSUserDefaults serves the task well.

MichaelScaria
  • 1,560
  • 2
  • 16
  • 25
-1

I think for your specific case, storing the refreshRate in NSUserDefaults would be the way to go. You could then retrieve it from anywhere within your app.

Saving an NSNumber (an object) to NSUserDefaults:

[[NSUserDefaults standardUserDefaults] setObject:refreshRate forKey:@"savedRefreshRate"];

Retrieving an NSNumber from NSUserDefaults:

NSNumber refreshRate = [[NSUserDefaults standardUserDefaults] objectForKey:@"savedRefreshRate"];

That code should work for your situation, but I'm still going to post a link to the class reference. You should get used to reading from them as it is an invaluable resource when learning to develop for iOS.

See NSUserDefaults class reference for more information.

rog
  • 5,351
  • 5
  • 33
  • 40
  • If I use NSUserDefaults, will my current info controller get a notification when a user changes the settings to a different poll interval. IE. if I start polling at 10 second intrevals and the user changes it to 30 seconds I need to inform the CurrentViewConrtoller. – lostintranslation May 20 '13 at 16:11
  • There are a few ways to be notified when an object's properties or ivars change; one of these ways is called Key Value Observation and it is a very well designed way to be notified when a particular property/ivar you are interested in changes. It is similar to using NSNotifications, but with slightly different use cases. See [Key Value Observing Programming Reference](http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html) for more information. – Ric Perrott May 20 '13 at 16:21
  • Yes, KVO would work well for that. I've suggested `NSUserDefaults` so you can save your `refreshRate` if you close/relaunch your app. To the two down-voters, would appreciate an explanation so I can improve/delete my answer. – rog May 20 '13 at 16:30