LettersBa
LettersBa

Reputation: 767

id to NSString return null when passing to another class

I have this code below that send the row to my another class:

[self performSegueWithIdentifier:@"MeInfos" sender:[NSString stringWithFormat:@"%d",(int)indexPath.row]];

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    MeInfosViewController *viewController = [[MeInfosViewController alloc] init];
    viewController.paramethersToLoad = (NSString*)sender;
    NSLog(@"Will receive -> %@",sender);
}

In my another class (MeInfoViewController) I have:

.h

@property (nonatomic, retain) NSString *paramethersToLoad;

.m

@synthesize paramethersToLoad;

-(void)viewDidAppear:(BOOL)animated{
    NSLog(@"What comes? -> %@",paramethersToLoad);
}

This code has a little problem, in console I receive the following messages:

Will receive -> 4
What comes? -> (null)

Why this is happening and what the best way to solve it?

Upvotes: 0

Views: 59

Answers (2)

Ali Abbas
Ali Abbas

Reputation: 4277

Your prepareForSegue should look like this :

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([[segue identifier] isEqualToString:@"MeInfos"]) {
         // Get reference to the destination view controller
          MeInfosViewController *viewController = [segue destinationViewController]; 

           viewController.paramethersToLoad = (NSString*)sender;
     }
}

Upvotes: 2

Chris Vig
Chris Vig

Reputation: 8792

The view controller you're creating in prepareForSegue: isn't the instance that the segue will actually load. The view controller that actually gets displayed is created by the iOS runtime, and is passed to you as a property in the segue.

What you want is:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    MeInfosViewController *viewController = (MeInfosViewController *)segue.destinationViewController;
    viewController.paramethersToLoad = (NSString *)sender;
}

Upvotes: 2

Related Questions