Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Can't pass data

I am not able to pass data between two views that I have. I have the code below.


In my first .h file


@interface themes : UIViewController {


ViewController *viewcontrollerdata;

IBOutlet UILabel *textfield;

}

@property (nonatomic, retain)ViewController *viewcontrollerdata;


@end


In my first .m file


@synthesize viewcontrollerdata;


- (IBAction)menu:(id)sender {


ViewController *second = [[ViewControlleralloc] initWithNibName:nilbundle:nil];

self.viewcontrollerdata = second;

viewcontrollerdata.passedValue = textfield.text;


[selfdismissViewControllerAnimated:YEScompletion:nil];


}




In my second .h file


@interface ViewController : UIViewController {

IBOutlet UILabel *label;

NSString *passedValue;

}

@property (nonatomic, retain)NSString *passedValue;



@end



In my second .m file


@synthesize passedValue;



- (void)viewDidLoad

{

label.text = passedValue;

}


What is happening is that the label where I am supposed to receive my data is invisible. Any help is appreciated.

xcode-OTHER, OS X Mountain Lion (10.8.4)

Posted on Nov 16, 2013 11:15 PM

Reply
3 replies

Nov 17, 2013 9:23 AM in response to psonthalia

Are you sure that is a complete working code sample?


1. Why is (IBAction)menu not in the first header?


2. You are creating an instance of your ViewController class but not presenting it.


3. You do not specify a NIB name when instantiating ViewController. It appears you have added a label in the designer so the layout must be in some NIB or storyboard.


4. If you are using a storyboard with segues you should not be instantiating ViewController yourself. You should pass the data in prepareForSegue.


5. What Xcode version are you using? I notice viewcontrollerdata is both a public interface variable and a property, and you are manually synthesizing.

Nov 17, 2013 7:19 PM in response to psonthalia

You answered #1 and 5. What about #2, 3, 4?


Simple example. I encourage you to create a new test project using the Single View Application template, with only this, to understand how it works. Then apply what you have learned to your real project.


ViewController: contains a label and a button.

SecondViewController: contains a label.

Modal segue between ViewController button and SecondViewController.


When SecondViewController appears, the label text will match the label text on ViewController.

Don't forget to assign the storyboard second view controller custom class in the identity inspector, and connect the label outlets on both view controllers.


// ViewController.h

#import <UIKit/UIKit.h>


@interface ViewController : UIViewController


@property (weak, nonatomic) IBOutletUILabel *sourceLabel;


@end


// ViewController.m

#import "ViewController.h"

#import "SecondViewController.h"


@implementation ViewController


- (void)viewDidLoad

{

[superviewDidLoad];

self.sourceLabel.text = @"Hello";

}


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

{

((SecondViewController *)(segue.destinationViewController)).labelText = self.sourceLabel.text;

}


@end


// SecondViewController.h

#import <UIKit/UIKit.h>


@interface SecondViewController : UIViewController


@property (weak, nonatomic) IBOutletUILabel *destinationLabel;


@property NSString *labelText;


@end


// SecondViewController.m

#import "SecondViewController.h"


@implementation SecondViewController


- (void)viewDidLoad

{

[superviewDidLoad];

self.destinationLabel.text = self.labelText;

}


@end

Can't pass data

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.