MasterViewController.m
#import "DetailViewController.h"
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"DetailViewControllerSeque"]) {
DetailViewController *detailView = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
theList = [app.listArray objectAtIndex:indexPath.row];
detailView.theList = theList;
// String to pass to DetailViewController
detailView.string2pass = @"this is a passing string";
}
}
DetailViewController.h
NSString *string2pass;
@property (retain, nonatomic) NSString *string2pass;
DetailViewController.m
NSLog(@"%@", string2pass);
Output: (null)
What I am doing wrong?
First you need to add
NSString
property to yourSecondViewController.h
file:and then in
FirstViewController.m
file createSecondViewController
object and pass to it whatever string you want:Log string in
SecondViewController
Unless you have this in your implementation, it won't work as you expected.
..or you can fix it by deleting the line:
Your log is logging the value of string2pass variable you declared. But there is another variable _string2pass.
The @property you declared, is backed by a variable name _string2pass if you don't explicitly write a @synthesize statement. Not writing an @sythesize statement is the same as declaring one like so:
Use
self.string2pass
inNSLog
. When we use self. to access a property then the getter/setter is called.In your DetailViewController.h only you need to define this -
In your DetailViewController.m
This will work for you.