1

I'm sure there is a simple explanation for this. I am looking to transfer the data (not store) but transfer the text the user types into a text field and display it as a UILabel in another ViewController. I already know how to convert text entered by the user into a label on the same viewcontroller. I guess my problem is importing.

the .h:

@interface ViewController : UIViewController {
IBOutlet UITextField *firstPerson;
IBOutlet UITextField *secondPerson;
IBOutlet UIButton *calculateButton;
NSString *firstName;
NSString *secondName;
}
@property (nonatomic, retain) IBOutlet UITextField *firstPerson;
@property (nonatomic, retain) IBOutlet UITextField *secondPerson;
@property (nonatomic, retain) NSString *firstName;
@property (nonatomic, retain) NSString *secondName;
@property (nonatomic, retain) IBOutlet UIButton *calculateButton;
-(IBAction)calculate;
@end

the .m:

-(IBAction)calculate {
//Linked to UIButton
//This is the first View Controller.
//    firstName = firstPerson.text;
//    secondName = secondPerson.text;
secondViewController = [[ShowStats alloc] init];
}

my secondview controller .m (ShowStats):

#import "ShowStats.h"
#import "ViewController.h"
- (void)viewDidLoad
{
firstName = firstPerson.text;
secondName = secondPerson.text;


[super viewDidLoad];
}

Many thanks! EDIT

ViewController.h

#import <UIKit/UIKit.h>
#import "ShowStats.h"

@interface ViewController : UIViewController {
IBOutlet UITextField *firstPerson;
IBOutlet UITextField *secondPerson;
IBOutlet UIButton *calculateButton;
//NSString *firstName;
// NSString *secondName;
}
@property (nonatomic, retain) IBOutlet UITextField *firstPerson;
@property (nonatomic, retain) IBOutlet UITextField *secondPerson;
//@property (nonatomic, retain) NSString *firstName;
//@property (nonatomic, retain) NSString *secondName;
@property (nonatomic, retain) IBOutlet UIButton *calculateButton;
-(IBAction)calculate;
@end

ViewController.m

#import "ViewController.h"
#import "ShowStats.h"

@implementation ViewController
@synthesize firstPerson, secondPerson;
//@synthesize firstName, secondName;
@synthesize calculateButton;
ShowStats *secondViewController;

-(IBAction)calculate {
secondViewController = [[ShowStats alloc] init];
secondViewController.firstName = firstPerson.text;
}

ShowStats.h

@interface ShowStats : UIViewController{

IBOutlet UILabel *nameStats;
}
@property (nonatomic, retain) IBOutlet UILabel *nameStats;
@property (nonatomic, retain) NSString *firstName;
@property (nonatomic, retain) NSString *secondName;
@end

ShowStats.m

- (void)viewDidLoad
{
nameStats.text = [NSString stringWithFormat:@"%@", firstName];    
//secondLabel.text = self.secondName;


[super viewDidLoad];
}

3 Answers3

1

Make these properties in ShowStats class

@property (nonatomic, retain) NSString *firstName;
@property (nonatomic, retain) NSString *secondName;

and change this to this

-(IBAction)calculate {
     secondViewController = [[ShowStats alloc] init];
     secondViewController.firstName = firstPerson.text;
     secondViewController.secondName = secondPerson.text;
}

then set these strings to UILablel in your viewDidLoad

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
0

In ShowStats.h, add the following:

@property(nonatomic, copy) NSString* firstName;
@property(nonatomic, copy) NSString* secondName;

In ShowStats.m add/update the following:

@synthesize firstName, secondName;

//...

- (id) init {
    if (self = [super init]) {
        self.firstName = nil;
        self.secondName = nil;
        //...
    }

    return self;
}

- (void) dealloc {
    self.firstName = nil;
    self.secondName = nil;
    //...

    [super dealloc];
}

- (void)viewDidLoad {
    //or even better, do this in viewWillAppear instead
    firstLabel.text = self.firstName;
    secondLabel.text = self.secondName;
    //...


    [super viewDidLoad];
}

Finally, in ViewController.m, implement calculate like this:

-(IBAction)calculate {
    //Linked to UIButton
    //This is the first View Controller.
    //    firstName = firstPerson.text;
    //    secondName = secondPerson.text;
    secondViewController = [[ShowStats alloc] init];
    secondViewController.firstName = firstPerson.text;
    secondViewController.secondName = secondPerson.text;

    //display the view controller here

    [secondViewController autorelease];
}
aroth
  • 54,026
  • 20
  • 135
  • 176
  • Thank you, I am not getting any errors. But it still won't display the text in the label. I am going to keep playing around with it though. – ZachBamberger Feb 10 '12 at 05:16
  • @ZachBamberger - How are you displaying the second view controller? Can you post the code you have for that part? – aroth Feb 10 '12 at 05:33
  • nameStats.text = [NSString stringWithFormat:@"%@", self.firstName]; – ZachBamberger Feb 10 '12 at 05:38
  • What do you mean? I just have it "Modal" after the user pushes calculate. – ZachBamberger Feb 10 '12 at 05:39
  • So you're calling `[self presentModalViewController:secondViewController animated:YES]` at some point after `calculate` returns? – aroth Feb 10 '12 at 05:57
  • Yeah, and then when I call to display the label. It shows up as (null). – ZachBamberger Feb 10 '12 at 05:59
  • Also, I set up the ModalViewController in StoryBoard. Would that suffice? Or is that my problem? – ZachBamberger Feb 10 '12 at 06:14
  • @ZachBamberger - That is *probably* your problem. If it is set up in the storyboard, then the transition to the second view controller is probably not using the instance you created in `calculate`. This is simple to test, just add `[self presentModalViewController:secondViewController animated:YES]` to `calculate`, and comment out the other code you are using to present the second view controller. If that works then the storyboard transition is the problem. – aroth Feb 10 '12 at 06:17
  • I am getting a black screen when I do that. – ZachBamberger Feb 10 '12 at 06:21
  • For whatever reason it isn't saving the user input in the firstPerson.text, or perhaps it is, but it's not putting it into the string in secondViewController. – ZachBamberger Feb 10 '12 at 06:23
  • Also, I must be getting some sleep. I am extremely tired. I will check on this in the morning, and hopefully something! Thank you so much for your help. Really appreciated it. I'll keep you updated. :D – ZachBamberger Feb 10 '12 at 06:25
  • @ZachBamberger - Hm...then you might need to replace the `[[ShowStats alloc] init]` with `[[ShowStats alloc] initWithNibName:...]`, as discussed [here](http://stackoverflow.com/questions/2224077/when-do-i-initialize-a-view-controller-with-initwithnibname). Or alternately, look [here](http://stackoverflow.com/questions/8261360/how-to-properly-use-modal-view-controller-with-the-xcode-4-2-storyboard) for tips on how to get the `ShowStats` controller from the storyboard segue; you can grab a reference, and then set `firstName` and `secondName` appropriately. – aroth Feb 10 '12 at 06:25
0

If it is a navigation then in your SecondViewController you can call:

ViewController *viewController = [self.navigationController.viewControllers objectAtIndex:0];
firstName = [[viewController firstPerson] text];
secondName = [[viewController secondPerson] text];

Or you could do the following for a Single View app:

AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
ViewController *viewController = [appDelegate viewController];
firstName = [[viewController firstPerson] text];
secondName = [[viewController secondPerson] text];

* EDIT *

Add two labels to your .h file (nonatomic, retain) & synthesize in .m . Initialize the labels in viewDidLoad then set them (assuming they are called label1 and label2):

[label1 setText:firstName];
[label2 setText:secondName];
brendan
  • 1,705
  • 1
  • 18
  • 24
  • Thank you, I am not getting any errors. But it still won't display the text in the label. I am going to keep playing around with it though. – ZachBamberger Feb 10 '12 at 05:16
  • Tried that as well as nameStats.text = [NSString stringWithFormat:@"%@", firstName]; neither of them work, with setText label shows up empty. With stringWithFormat it shows up as null. – ZachBamberger Feb 10 '12 at 05:45
  • Did you set the owner to be the labels in IB? – brendan Feb 10 '12 at 05:47
  • Can you edit your initial post to show the entirety of ShowStats.h and all places in the .m where anything with a label or firstName/secondName is modified? – brendan Feb 10 '12 at 05:50
  • Okay Edited... Hopefuly that helps – ZachBamberger Feb 10 '12 at 05:58