在两个 DetailViewController 之间传递 NSString [英] 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?
推荐答案
除非你在你的实现中有这个,否则它不会像你预期的那样工作.
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;
您的日志记录了您声明的 string2pass 变量的值.但是还有另一个变量_string2pass.
Your log is logging the value of string2pass variable you declared. But there is another variable _string2pass.
NSLog(@"%@", string2pass);
如果您没有明确地编写@synthesize 语句,您声明的@property 由变量名 _string2pass 支持.不写@sythesize 语句与声明这样的语句是一样的:
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;
这篇关于在两个 DetailViewController 之间传递 NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!