Pass NSString value to another ViewController in Objective C

℡╲_俬逩灬. 提交于 2019-12-12 03:29:31

问题


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.


回答1:


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.




回答2:


try this code

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



回答3:


Try this

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

And Print

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


来源:https://stackoverflow.com/questions/41102084/pass-nsstring-value-to-another-viewcontroller-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!