Question

I am super new to programming in general. I am trying to make a very simple app.

I am trying to pass data from one viewcontroller to another using a storyboard. On my first screen I have a text field and a button then after you press the button i want the second screen's label to be updated with whatever text you put in the text field.

Here is some of my code:

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController

@synthesize secondviewData;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{


    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)passData:(UIButton *)sender {

//    SecondViewController *second = [[SecondViewController alloc] initWithNibName:nil bundle:nil];

    self.secondviewData = secondviewData;
    secondviewData.passedValue = input.text;

//    [self presentViewController:second animated:YES completion:nil];

    homeLabel.text = input.text;

}
@end

#import "SecondViewController.h"
#import "FirstViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize passedValue;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{



    label.text = passedValue;

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

Here's a link to my code: http://oberober.com/passingData/

So far the label on the second screen keeps going blank no matter what I enter on the first screen's text field.

I feel like I am missing an important concept? Any tips or tutorials I can look at?

Thanks ahead of time, Casper

Était-ce utile?

La solution

You pass the data by using the prepareForSegue method of the first controller. Here's an example from one of my apps that shows a vehicle object being passed to the second controller.
CHRBodyViewController is the class of the second controller.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    CHRBodyViewController *body = [segue destinationViewController];
    body.vehicle = _vehicle;
    body.updated = YES;

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top