Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
145 views
in Technique[技术] by (71.8m points)

ios - Pass NSString between two DetailViewController

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unless you have this in your implementation, it won't work as you expected.

@synthesize string2pass = string2pass;

..or you can fix it by deleting the line:

NSString *string2pass;

Your log is logging the value of string2pass variable you declared. But there is another variable _string2pass.

NSLog(@"%@", 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:

@synthesize string2pass = _string2pass;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...