我使用下面的代码来选择 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]
这取决于您的属性是否可选(也许您必须删除?或!)
关于ios - 使用 Swift 和 Storyboard 在两个 UIViewController 之间传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27420772/