我使用下面的代码选择 UITableView 在 UIViewController 之间传递数据
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showRecipeDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
RecipeDetailViewController *destViewController = segue.destinationViewController;
destViewController.recipeName = [recipes objectAtIndex:indexPath.row];
}
}
如何使用 swift 对 tableView 选定的数据执行相同的操作?
我尝试过以下简单传递的方法,该方法有效
但如何复制上述案例。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var detailsVC = segue.destinationViewController as SecondViewController
detailsVC.passedString = "hello"
}
这应该有效:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "showRecipeDetail" {
var detailsVC = segue.destinationViewController as SecondViewController
let indexPath = tableView.indexPathForSelectedRow()
detailsVC.passedString = recipes![indexPath?row]
//or try that if above doesn't work: detailsVC.passedString = recipes[indexPath?row]
}
}
}
该行可能需要修改:
destViewController?.destViewController.recipeName = recipes![i]
这取决于你的属性是否可选(也许你必须删除?或!)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)