使用Swift和Storyboard在两个UIViewControllers之间传递数据 [英] Passing data between two UIViewControllers using Swift and Storyboard

查看:116
本文介绍了使用Swift和Storyboard在两个UIViewControllers之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码选择UITableView在UIViewControllers之间传递数据

I was using below code to selected UITableView to pass data between 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];
    }
}

如何使用swift与tableView选择的数据做同样的事情?

How to do same thing using swift with tableView selected data?

我已尝试过以下方法进行简单传递,但是如何复制上述案例。

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"

}


推荐答案

这应该有效:

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]

这取决于你的属性是否可选(也许你必须删除?或者!)

it depends is you properties optional or not (maybe you have to remove ? or !)

这篇关于使用Swift和Storyboard在两个UIViewControllers之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆