Paulius Dragunas
Paulius Dragunas

Reputation: 1712

NSString not being passed correctly after segue

I am using a storyboard and a conditional segue that seems to work properly. However, when I pass the string to the new view controller, it shows up as null in the new view controller when I try to print it out with NSLog.

VC1:

[self performSegueWithIdentifier: @"userDetailsSegue" sender: self];


    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"userDetailsSegue"])
{
    UIStoryboard *mainStoryboard1 = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    tableView1 * secondView = (tableView1*)[mainStoryboard1 instantiateViewControllerWithIdentifier:@"userDetails"];
    //tableView1 * secondView = [[tableView1 alloc] init];
    [secondView setNetID:userNameTextField.text];
    //NSLog(secondView.netID);
    NSLog(@"Works");
}
}

VC2.h:

#import "ViewController.h"
@interface tableView1 : UITableViewController
{
    NSString *netID;
}

@property (nonatomic, retain) IBOutlet NSString *netID;

@end

VC2.m:

- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"my netID is: %@", netID);
NSLog(@"test:");}

output:

2013-04-25 10:23:56.576 HwPlusPlus[14240:c07] Works
2013-04-25 10:23:56.579 HwPlusPlus[14240:c07] my netID is: (null)
2013-04-25 10:23:56.580 HwPlusPlus[14240:c07] test:

I have attempted to set netID = [[NString alloc] init]; in VC2 but that does not help either. Any idea what I am doing wrong? It pulls up the second view controller just find

Upvotes: 1

Views: 108

Answers (3)

SpaceDust__
SpaceDust__

Reputation: 4914

Next time please use capital letter for the first char of your VC, and try to choose meaningful or unique names for VCs

VC1:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"userDetailsSegue"])
    {
        tableView1 * secondView =segue destinationViewController];
        secondView.netID=userNameTextField.text;
    }
}

VC2.h:

#import "ViewController.h"
@interface tableView1 : UITableViewController
{
    NSString *netID;
}

@property (nonatomic, strong)NSString *netID;

@end

VC2.m:

@implementation tableView1
@synthesize netID;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"my netID is: %@", self.netID);
}

Upvotes: 1

danypata
danypata

Reputation: 10175

You should try this:

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
       if ([segue.identifier isEqualToString:@"SegueIdentifier"]) {
           UIViewController *viewController = segue.destinationViewController;
           // you can also use custom view controllers but you have to add a cast to that custmo view controller
           [viewController setNetID:userNameTextField.text]; 
       }
    }

Also make sure that the value passed to the UIViewController is not nil.

Upvotes: 0

matt
matt

Reputation: 535566

The problem is this line:

tableView1 * secondView = (tableView1*)[mainStoryboard1 instantiateViewControllerWithIdentifier:@"userDetails"];

You are now making, unnecessarily, an extra view controller. Don't do that! It is the wrong view controller and it will never appear in the interface; you are just making it, setting it up, and throwing it away, pointlessly. The segue in prepareForSegue already has a destinationViewController set to the view controller that is about to appear. Use that:

tableView1 * secondView = (tableView1*)segue.destinationViewController;

Upvotes: 0

Related Questions