2016-12-12 1 views
0

J'essaie de transmettre la valeur NSString à un autre ViewController. Mais j'obtiens null au lieu de la valeur. Voici mon code. FirstViewController.m:Transmettre la valeur NSString à un autre ViewController dans Objective C

NSString cellID = @"66"; 
    SecondViewController *apiViewController = [[SecondViewController alloc] init]; 
       apiViewController.strMessage=cellID; 
       [self.navigationController pushViewController:apiViewController animated:YES]; 

SecondViewController.h:

@interface SecondViewController : UIViewController{ 
    NSString *strMessage; 

} 
@property(nonatomic,retain) NSString *strMessage; 
@end 

SecondViewController.m:

@implementation SecondViewController 
@synthesize strMessage; 
NSLog(@"%@", strMessage); 
@end 

je l'ai fait l'importation. J'ai assigné une variable avant de pousser. Mais il retourne quand même null. Comment puis-je faire ceci? Des conseils, des idées. Je vous remercie.

+0

regard sur prepareForSegue. https://developer.apple.com/reference/uikit/uiviewcontroller/1621490-prepareforsegue –

+0

Il y a déjà tellement de sujets pour ce sujet. Quel est le point de discuter d'un sujet de 8 ans? –

+0

@ElTomato Je les ai tous essayés et la plupart d'entre eux ont écrit pour la connexion nib. Je n'ai pas besoin de ça. – JohnLemon

Répondre

0

Essayez cette

SecondViewController *apiViewController = [[SecondViewController alloc] init]; 
apiViewController.strMessage=cellID; 
[self.navigationController pushViewController:apiViewController animated:YES]; 

Et Imprimer

NSLog(@"%@", self.strMessage); 
0

Essayez d'attribuer la propriété avant vous appuyez sur le contrôleur de vue.

NSString cellID = @"66"; 
    LineChartViewController *apiViewController = [[LineChartViewController alloc] init]; 
       apiViewController.strMessage=cellID; 
       [self.navigationController pushViewController:apiViewController animated:YES]; 

De plus, vous devez appeler le NSLog à partir d'une fonction dans votre mise en œuvre. La fonction viewDidLoad, par exemple, car il s'agit d'un UIViewController qui est poussé sur une pile UINavigationController.

0

essayer ce code

SecondViewController *apiViewController = [[SecondViewController alloc] init]; 
    apiViewController.strMessage=cellID; 
        [self.navigationController pushViewController:apiViewController animated:YES];