如何使用 -prepareForSegue 中的属性? [英] How do I use a property in -prepareForSegue?
问题描述
这是我的代码,发布在视图 1 中:
Here is my code, posted in view1:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([segue.identifier isEqualToString:@"toLevels"])
{
levelViewController *vc = [segue destinationViewController];
vc.currentEnchantmentSelected = self.title;
}
}
这不会将self.title"的值发送到levelViewController"中的变量.为什么是这样?如果我使用字符串,它会发送它.
This doesn't send the value of "self.title" to the variable in "levelViewController". Why is this? It sends it if I use a string.
编辑(levelViewController
):
@synthesize enchantment = _enchantment;
- (enchantmentViewController *)enchantment
{
if (!_enchantment){
_enchantment = [[enchantmentViewController alloc] init];
}
return _enchantment;
}
@property enchantment
在头部声明.
推荐答案
所以从聊天中,问题是你在 didSelectRowAtIndexPath:
中设置了这个属性,实际上是在 之后调用 prepareForSegue:sender:
带有故事板.(当 segue 附加到表格中的单元格时)
So from the chat, the issue was that you were setting this property in didSelectRowAtIndexPath:
, which is actually called after prepareForSegue:sender:
with storyboards. (when the segue is attached to a cell in a table)
所以解决方案只是直接在 prepareForSegue:sender:
中完成这项工作.sender
将是被点击的单元格.如果您需要该单元格的 indexPath,您可以使用:
So the solution is just to do that work directly in prepareForSegue:sender:
instead. The sender
will be the cell that was tapped. And if you need the indexPath for that cell you can use:
[self.tableView indexPathForSelectedRow]
祝应用程序好运.
这篇关于如何使用 -prepareForSegue 中的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!