-2

i want to get the username (txtUsername) from my loginView controller (ViewController) to SecondViewController in order to print the message "Welcome Username". I am using Segue (login_success) between the view controller. In my SecondViewController I have NSString (stringUsername) to store the username from my FirstViewController. Here is my code in SecondViewController:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    NSString *segueIdentifier = [segue identifier];
    if ([segueIdentifier isEqualToString:@"login_success"]) {
        ViewController *homeController = (ViewController *)[segue destinationViewController];
        homeController.txtUsername.text = _stringUsername;
    }
}

When i set the break point it gives the output as "Printing description of self->_stringUsername: (NSObject) NSObject = "

Rob
  • 415,655
  • 72
  • 787
  • 1,044
Christine
  • 514
  • 1
  • 5
  • 14

1 Answers1

1

A couple of observations:

  1. You say:

    I want to get the username (txtUsername) from my loginView controller(ViewController) to SecondViewController...

    In that case, your prepareForSegue should be in the source view controller, not the destination view controller. And the segue.destinationViewController would be SecondViewController, not ViewController.

    Frankly, I'm having trouble reconciling your intent, quoted above, with your code snippet (which is looks like you were trying to do a segue from SecondViewController to ViewController).

  2. When you update the destination view controller, you should not try to set the text property of the IBOutlet. The prepareForSegue shouldn't try to use IBOutlet references at all, because when prepareForSegue has been called, the destination's view has not yet been instantiated and all of the IBOutlet references are likely nil.

    Instead, prepareForSegue in the source view controller should just set a NSString property in the destination controller. The responsibility for populating the text property of the IBOutlet on the basis of the NSString property falls upon viewDidLoad in the destination view controller.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • 1
    thank you so much rob for taking out ur time to correct me as i am new here. I have more confusion but i will try to figure it out as thats the way of learning. thanks again. – Christine Oct 07 '14 at 22:14