NSString value not retained
I'm trying to assign data from a string to another string within a different viewcontroller however it seems that the data is not retained - i g开发者_StackOverflow社区et a null response in NSLog. I would like to know why, thanks ..
Try changing the order a bit, as below, and use retain instead of copy:
SchoolDetailViewController *schoolController = [[SchoolDetailViewController alloc]initWithNibName:nil bundle:nil];
schoolController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
schoolController.courseDetails = @"passing new content";
[self presentModalViewController:schoolController animated:YES];
NSLog(@" %@",schoolController.courseDetails); // 'passing new content' is shown
.h
NSString *courseDetails;
@property (nonatomic, retain) NSString *courseDetails;
.m
@synthesize courseDetails;
- (void)viewDidLoad {
NSLog(@" text : %@",courseDetails); // returns null ... why?
[super viewDidLoad];
}
This should work.
Well that is because the viewDidLoad method is called when you present the view controller with or without animation.
So just flip these 2 statements
[self presentModalViewController:schoolController animated:YES];
schoolController.courseDetails = @"passing new content";
like this
schoolController.courseDetails = @"passing new content";
[self presentModalViewController:schoolController animated:YES];
And then check the results once again...
精彩评论