views:

43

answers:

3

Hi

Im using tabs to switch between two view controllers.

How do I retrieve a float in the secondviewcontroller, thats been initiated in the firstviewcontroller? should i make some sort of global variable? Where and how do I do this?

Thanks guys :)

A: 

Global variables are never desirable, I would strongly recommend using some messaging pattern, s.th. SecondViewController and FirstViewController can synchronize whenever they change anything of interest to the other. On first glance, I only found this guideline http://www.informit.com/articles/article.aspx?p=1398611 telling about messaging-patterns in cocoa, I guess there will already be sample-implementations for iPhone floating around.

Simpzon
A: 

Use AppDelegate For This

+(BOOL)SetData:(float)Value
{
 GlobalValue=Value;
}

+(float)ReturnData
{
 return GlobalValue;
}

and Call like This

[YourAppDelegate ReturnData];
Ashish Mathur
Not sure if this should be added as a new question?Ok Ive tried using NSUserDefaults. First I tried with a string and it worked, now Im struggling to do the same with float. In my firstviewcontroller.h`IBOutlet UITextField *nameField;IBOutlet UILabel *greeting;float RWI; float Liters;@property(nonatomic) float *Liters;`@property(nonatomic, retain); IBOutlet UILabel *greeting;@property(nonatomic, retain) IBOutlet UITextField *nameField;-(IBAction) updatePrefs:(id) sender; @property(nonatomic) float *RWI; `
anneke
In .m file`float RWI;//@synthesize RWI; should this be here?@synthesize Liters;@synthesize greeting; -(IBAction) updatePrefs:(id) sender{NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];[prefs setObject:nameField.text forKey:@"greeting"];[prefs setFloat:20 forKey:@"Liters"];[prefs synchronize];For secondviewcontroller.hIBOutlet UILabel *greeting;float *Liters;}@property(nonatomic, retain) IBOutlet UILabel *greeting;@end
anneke
In .m@implementation secondviewcontroller@synthesize greeting;@synthesize Liters;- (void)viewDidLoad {[super viewDidLoad];NSString *prefs = [[NSUserDefaults standardUserDefaults] objectForKey:@"greeting"];float Liters = [prefs floatForKey:@"Liters"];greeting.text = prefs; } Any help would be appreciated! :D
anneke
This should be a new question... No chance someone will answer that within a thread of comments.
Toastor
A: 

You could make that variable a property of your app delegate, which would be accessible from anywhere within your app. If you don't want that for whatever reason, you could create a "helper" singleton to keep such variables and make them properties again.

Toastor
Ok so would I be going in my app delegate.h float RWI; @property (nonatomic, retain) float *RWI;` ? and how do i call it from my view controllers?
anneke
You cannot retain a float, otherwise yes. In your viewcontroller, do: `float RWI = [[[UIApplication sharedApplication] delegate] RWI];`
Toastor
sorry im real new to this.. how do i send from my firstviewcontroller and how do I then retrieve it in my secondviewcontroller?
anneke
the line of code in my previous comment is the "receiving" code. It calls the getter of your app delegate's new property and returns its value. To "send", you set it in your first viewcontroller like this: `[[[UIApplication sharedApplication] delegate] setRWI:foobar];`, where foobar contains the value you would like to set.
Toastor
To explain this a little further: What you're actually doing is calling methods to set and retrieve an instance variable's value which have been automatically generated for your property - that's what @synthesize is there for. So, you're not really accessing the variable itself. The other stuff (UIApplication sharedApplication...) is calling a class method to retrieve a pointer to your app delegate, so you could send it the getter and setter calls in the first place.
Toastor