在两个DetailViewController之间传递NSString
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);
输出:(null)
我做错了什么?
除非您在实施中有这个function,否则它将无法按预期工作。
@synthesize string2pass = string2pass;
..或者你可以通过删除行来修复它:
NSString *string2pass;
你的日志正在logging你声明的string2passvariables的值。 但是还有另外一个variables_string2pass。
NSLog(@"%@", string2pass);
如果您没有明确写入@synthesize语句,则您声明的@property由variables名称_string2pass支持。 不写一个@sythesize语句就像这样声明一样:
@synthesize string2pass = _string2pass;
在NSLog
使用self.string2pass
。 当我们使用自我。 访问一个属性,然后调用getter / setter。
在你的DetailViewController.h只有你需要定义这个 –
@property (strong, nonatomic) NSString *string2pass;
在你的DetailViewController.m中
@synthesize string2pass
这将为你工作。
首先,您需要将NSString
属性添加到您的SecondViewController.h
文件中:
@property (nonatomic, copy) NSString *myString;
然后在FirstViewController.m
文件中创buildSecondViewController
对象并将其传递给它所需的任何string:
SecondViewController *secondViewcontroller = [[SecondViewController alloc] initWithNibName:@"ACEViewController" bundle:nil]; secondViewcontroller.myString = @"WhateverYouNeedToPass";
在SecondViewController
loggingstring
NSLog(@"%@", self.myString);