问题
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