Pass NSString value to another ViewController in O

2019-09-06 06:43发布

I am trying to pass NSString value to another ViewController. But I get null instead of the value. Here is my code. FirstViewController.m:

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

I did import. I assigned variable before pushing. But it anyway returns null. How can I do this? Any tips, ideas. Thank you.

3条回答
兄弟一词,经得起流年.
2楼-- · 2019-09-06 06:59

Try this

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

And Print

NSLog(@"%@", self.strMessage);
查看更多
在下西门庆
3楼-- · 2019-09-06 07:02

Try assigning the property before you push the view controller.

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

Furthermore, you need to call the NSLog from within a function in your implementation. The viewDidLoad function, for example, as this is a UIViewController that is being pushed on a UINavigationController stack.

查看更多
成全新的幸福
4楼-- · 2019-09-06 07:16

try this code

SecondViewController *apiViewController = [[SecondViewController alloc] init];
     apiViewController.strMessage=cellID;
                     [self.navigationController pushViewController:apiViewController animated:YES];
查看更多
登录 后发表回答