1

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];
}
2
  • How is the SecondView controller being instantiated? I'm assuming that the FirstViewController does? Commented Apr 10, 2012 at 0:03
  • You can also make the assign property of the text variable and can use it in another class. Commented Apr 16, 2012 at 11:19

4 Answers 4

3

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"];
Sign up to request clarification or add additional context in comments.

3 Comments

How would I go about reseting the label in the secondviewcontroller, if for example the user goes back to the firstviewcontroller and types in a new number to calculate?
just do this in -(void)ViewdidAppear{mylbl.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"name"]}
I would say "easy," but definitely not the best. Most of the time they are a bad idea - c2.com/cgi/wiki?GlobalVariablesAreBad
0

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.

Comments

0

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; 
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.