1

I'm trying to pass data from 1 viewcontroller to another, but the NSString is getting lost somewhere. I apologize in advance, I feel like I'm missing something dumb here. I've read so many posts on this but I can't figure out why my NSStrings are getting lost between views. If anyone could give me a pointer I would really appreciate it:

ViewController1 passes payee.name to ViewController2 payeeNameText here:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"makePayment"]){
        VC2* paymentController = (VC2*)[segue destinationViewController];
        paymentController.payeeNameText = [payee.name copy];
    }
}

ViewController2 tries to use that data to display the payees name in payeeName UILabel:

- (void)viewDidLoad {

    [super viewDidLoad];
    NSLog(@" test %@",  self.payeeNameText);
   self.payeeName.text = [NSString stringWithFormat:@"%@",    self.payeeNameText];
    }

Its prints it out correctly in the NSLog statement, but the text field displays as null. I also print out the same variable later after I click a button and its null there as well.

This is my definition of payee in View Controller 2 header file:

@property (nonatomic, copy) NSString *payeeNameText;

and my definition of the text field:

@property (nonatomic) IBOutlet UILabel *payeeName;

If I set paymentController.payeeNameText to a string literal such as @"test" - everything works as expected.

Please if anyone can help me, I've been stuck on this for a while, I would greatly appreciate it. Thank you!

ldet
  • 71
  • 6
  • try using `payeeNameText` in `viewWillAppear` rather than `viewDidLoad` – Paulw11 Dec 15 '15 at 04:24
  • 2
    Are you sure that `self.payeeName` isn't `nil`? – rmaddy Dec 15 '15 at 04:24
  • Thank you everyone for your suggestions. I've updated the post, but I don't think it has anything to do with setup or location of assignment because if I hard code a literal string for self.payeeNameText in prepareForSegue everything works fine. – ldet Dec 15 '15 at 11:29
  • Try logging the value of `self.payeeName`. (As an aside, why are you using `stringWithFormat `?) – Phillip Mills Dec 15 '15 at 14:02

1 Answers1

0

It turns I was trying to display data that was being returned from an http get request, but I was displaying the modal segue on button click AND on data returned. So it appears there was a race condition and the data wasn't getting set properly.

First I changed my segue to "show" - this ended up showing me 2 separate screens 1 without the data which was then replaced with the one with the data (which is how I caught this error)

So the solution was to remove my segue from my button and attach it to my View Controller and then only call in 1 time after my get request returned.

ldet
  • 71
  • 6