1
votes

I know this question was asked about a 100 times but I just can't figure out what I'm doing wrong.

This ist my second iOS App. I want to modify a Label in my ViewControllerB by pressing a Button in my ViewControllerA.

  1. I made a Segue in Storyboard from my Button (ViewControllerA) to my ViewControllerB and called it "TestSegue".
  2. Wrote #import "ViewControllerB.h in my ViewControllerA.h
  3. I wrote this Code in my ViewControllerA.m :

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

if([segue.identifier isEqualToString:@"TestSegue"]){

ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
controller.TestLabel.text = @"TEST!";}}

In my ViewControllerB nothing happens with the TestLabel...

Can anybody see what I did wrong?

Thanks in advance

Edit: Works now thanks to rpledge.

New Code:

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

if([segue.identifier isEqualToString:@"TestSegue"]){

ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
controller.TestString = @"TEST!";}}
2

2 Answers

1
votes

It'l likely because the TextLabel UI element isn't created until viewDidLoad is run, which will happen after prepareForSegue:

I don't like exposing UI elements for views publicly anyway, I would suggest you add an @property for your NSString* to the destination view controller then set the UILabel.text in viewDidLoad:

0
votes

Don't assign it to the control, but to an instance variable.

@property (nonatomic) NSString *var;

And then in the viewDidLoad assign this to the control.