SNV7
SNV7

Reputation: 2593

Accessing variable values from one view in another

I'm having some trouble understanding how variable values are passed from one view to another. I have a UITextField in the firstview that the user enters a number into. When the user taps a button, that number is multiplied by 2 and the result is displayed on a UILabel in the second view. This is what I have thus far

FirstViewController.h

@interface FirstViewController : UIViewController{
UITextField *numberTextField;
NSNumber *aNumber;
}
@property (nonatomic, retain) IBOutlet UITextField *numberTextField;
@property (nonatomic) NSNumber *aNumber;
-(IBAction)calculate;
@end

FirstViewController.m

@implementation FirstViewController
@synthesize numberTextField,  aNumber;

-(double)doubleNumber{
double number = [numberTextField.text doubleValue] * 2;
return number;
}

-(IBAction)calculate{
self.aNumber = [NSNumber numberWithDouble:[self doubleNumber]];   
}

//more default code continues below

SecondViewController.h

#import "FirstViewController.h"

@interface SecondViewController : FirstViewController{
UILabel *numberLabel;
}
@property (nonatomic, retain) IBOutlet UILabel *numberLabel;
@end

SecondViewController.m

@implementation SecondViewController
@synthesize numberLabel;

- (void)viewDidLoad
{
[super viewDidLoad];
numberLabel.text = [NSString stringWithFormat:@"%@",aNumber];
}

Upvotes: 1

Views: 1326

Answers (4)

freelancer
freelancer

Reputation: 1658

Best and Easy Way to store value globally

set you object with your keyword

[[NSUserDefaults standardUserDefaults] setObject:@"Ajay" forKey:@"name"]; 

than get that object any where in you project

NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];

Upvotes: 3

Giuseppe Garassino
Giuseppe Garassino

Reputation: 2292

Mmm... Pay attention to not confuse views and viewControllers!!!

A viewController can manage more than a view. For example in your code you have a UITextField, a UILabel and probably a UIButton. These are all views that are managed by two viewsController (FirstViewController and SecondViewController).

As long as you have so few views to work with you can use just one viewController and pass the value you want to your UILabel directly:

- (void)calculateAndPassValue
{
    aNumber = [NSNumber numberWithDouble:[self doubleNumber]];
    numberLabel.text = [NSString stringWithFormat:@"%@",aNumber];
}

Otherwise, if your goal is passing variable values from one viewController to another. Well... There are many ways you can obtain that, for example:

  1. Creating an ivar.
  2. Using a singleton.
  3. Saving your data in NSUserDefault.
  4. Creating a database on disk.

First and second cases are good if you need to manage your data while your app is running. Third and fourth if you want to memorize your data for future use and retrieve them at next start up.

Try to search keys like ivar, singleton, NSUserDefault and you'll find many discussions and lines of sample code.

Upvotes: 0

5StringRyan
5StringRyan

Reputation: 3634

I'm assuming that the FirstViewController is instantiating the SecondViewController. If that is so, then you just pass aNumber to the SecondViewController using an additional property:

// add an additional property to the SecondViewController
@property (nonatomic, retain) NSNumber *aNumber;

When you instantiate the SecondViewController inside the FirstViewController, you just pass that value to the SecondViewController before you load it:

// inside FirstViewController 
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
secondViewController.aNumber = aNumber;

// inside SecondViewController
- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    numberLabel.text = self.aNumber; 
}

Upvotes: 0

Mike Z
Mike Z

Reputation: 4111

You should accomplish what you want by using a segue. Create a segue in your storyboard and then call it in your firstviewcontroller with - (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender. Then to pass data, import your secondviewcontroller.h file with a property for the value you want to pass and setup - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender.

In that method you can pass things by using [segue.destinationViewController ###call the setter for the property in your secondViewController.h file###];

If this isn't clear, or you need more help just let me know.

Upvotes: 0

Related Questions