[英]Passing data between two UIViewControllers using Swift and Storyboard
I was using below code to selected UITableView to pass data between UIViewControllers 我正在使用下面的代码选择UITableView在UIViewControllers之间传递数据
- (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];
}
}
How to do same thing using swift with tableView selected data? 如何使用swift与tableView所选数据进行相同的操作?
I have tried below approach for simple passing which is working 我已经尝试了以下方法进行有效的简单通过
but How to replicate above mentioned case. 但是如何复制上述情况。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
var detailsVC = segue.destinationViewController as SecondViewController
detailsVC.passedString = "hello"
}
This should work: 这应该工作:
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]
}
}
}
This line could require amendments: 此行可能需要修改:
destViewController?.destViewController.recipeName = recipes![i]
it depends is you properties optional or not (maybe you have to remove ? or !) 这取决于您的属性是否可选(也许您必须删除?或!)
Try this... If you're first view controller is embedded inside Navigation Controller, you can push the second view controller on didSelectRowAtINdexPath delegate, as below... 尝试此操作...如果您是第一个视图控制器嵌入在Navigation Controller内,则可以将第二个视图控制器推入didSelectRowAtINdexPath委托上,如下所示...
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
// Create a storyboard instance.....
var storyBoard = UIStoryboard(name: "Main", bundle: nil)
// Create a SecondViewController instance, from the storyboardID of the same.
var seconVC = storyBoard.instantiateViewControllerWithIdentifier("seconViewControllerID") as SecondViewController
seconVC.passedString = receipes[indexPath.row] as String
self.navigationController?.pushViewController(secondVC, animated: true)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.