我有一个具有表视图控制器和详细信息视图控制器的应用程序。我想将一个字符串的值传递给详细控制器中的另一个字符串。我使用情节提要,实际上,当我不使用它时,它可以工作,但是在另一个应用程序中。我堆放在这里,无法理解我做错了什么。有我的代码:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
DetailViewController *detailController = [[DetailViewController alloc]init];
detailController.descriptionStringShort = [[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:@"description"];
NSLog(@"%@", [[self.listOfPlaceDetails objectAtIndex:indexPath.row] objectForKey:@"description"]);
}
在控制台中,当我点击第一行控制器中的行时,我可以看到值,但是当我推送到另一个视图时,我只能在控制台中看到nil:
- (void)viewDidLoad
{
[super viewDidLoad];
self.myScrollView.contentSize = CGSizeMake(320, 960);
self.myTextView.text = self.descriptionStringShort;
[self.myScrollView addSubview:self.myTextView];
NSLog(@"%@ descriptor", self.descriptionStringShort);
}
控制台中没有nil。我究竟做错了什么?
任何建议,将不胜感激,谢谢!
您不应DetailedViewController
在使用segues时创建新的实例。
实现方法prepareForSegue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"YourSegueID"]) {
NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
NSString *string = [[self.listOfPlaceDetails objectAtIndex:indexPath.row]objectForKey:@"description"];
DetailedViewController *DVC = segue.destinationViewController; //Might need a cast
DVC.descriptionStringShort = string;
}
}
注意:此代码未经过类型检查,但是,它应该为您提供有关如何执行此操作的想法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句